Add a concrete example of an optional value. Fixes #2848.

This commit is contained in:
Carol (Nichols || Goulding) 2022-01-03 21:34:13 -05:00 committed by Carol (Nichols || Goulding)
parent 9e97ca7ba8
commit d3740fb7aa
2 changed files with 13 additions and 5 deletions

View File

@ -265,10 +265,16 @@ useful: `Option`.
This section explores a case study of `Option`, which is another enum defined
by the standard library. The `Option` type encodes the very common scenario in
which a value could be something or it could be nothing. Expressing this
concept in terms of the type system means the compiler can check whether youve
handled all the cases you should be handling; this functionality can prevent
bugs that are extremely common in other programming languages.
which a value could be something or it could be nothing.
<!-- Liz: I just got an issue from a reader requesting a concrete example
of this scenario, so I've added two sentences here. Please check to see if they
make sense! /Carol-->
For example, if you request the first of a list containing items, you would get
a value. If you request the first item of an empty list, you would get nothing.
Expressing this concept in terms of the type system means the compiler can
check whether youve handled all the cases you should be handling; this
functionality can prevent bugs that are extremely common in other programming
languages.
Programming language design is often thought of in terms of which features you
include, but the features you exclude are important too. Rust doesnt have the

View File

@ -183,7 +183,9 @@ useful: `Option`.
This section explores a case study of `Option`, which is another enum defined
by the standard library. The `Option` type encodes the very common scenario in
which a value could be something or it could be nothing. Expressing this
which a value could be something or it could be nothing. For example, if you
request the first of a list containing items, you would get a value. If you
request the first item of an empty list, you would get nothing. Expressing this
concept in terms of the type system means the compiler can check whether youve
handled all the cases you should be handling; this functionality can prevent
bugs that are extremely common in other programming languages.