cognitive-engineering-lab / rust-book

The Rust Programming Language: Experimental Edition
https://rust-book.cs.brown.edu
Other
593 stars 92 forks source link

Confusion of "type" and "value' #71

Closed seishun closed 1 year ago

seishun commented 1 year ago

URL to the section(s) of the book with this problem: https://rust-book.cs.brown.edu/ch19-04-advanced-types.html#the-never-type-that-never-returns

Description of the problem:

Rust has a special type named ! that’s known in type theory lingo as the empty type because it has no values.

Later it says:

As you might have guessed, continue has a ! value. That is, when Rust computes the type of guess, it looks at both match arms, the former with a value of u32 and the latter with a ! value.

Here, the loop never ends, so ! is the value of the expression.

How can ! be the value of something if it's a type that has no values?

Suggested fix:

Replace "value" with "type" where appropriate.

seishun commented 1 year ago

There are more cases of this confusion in other chapters. For example, in https://rust-book.cs.brown.edu/ch19-05-advanced-functions-and-closures.html#returning-closures:

In most cases where you might want to return a trait, you can instead use the concrete type that implements the trait as the return value of the function.

willcrichton commented 1 year ago

Fixed this in 780b7c9372a3581d08f4eeed814685b3dc753c2a.