book icon indicating copy to clipboard operation
book copied to clipboard

Seemingly contrary statements in 3.3

Open dennismarwood opened this issue 3 years ago • 2 comments

  • [x] I have checked the latest main branch to see if this has already been fixed
  • [x] I have searched existing issues and pull requests for duplicates

URL to the section(s) of the book with this problem: https://doc.rust-lang.org/book/ch03-03-how-functions-work.html#function-bodies-contain-statements-and-expressions

Description of the problem: Some clarification could be added to help differentiate the definition of functions, function bodies, function declaration, and function definitions.

The 3.3 section is describing statements vs expressions. A description of statements says "Statements are instructions that perform some action and do not return a value." Then below says "Statements do not return values."

But, in an apparent contradiction, it also says "Function definitions are also statements; the entire preceding example is a statement in itself." followed later by "Functions can return values to the code that calls them."

Suggested fix: A line defining the relationship of function bodies, function declarations, and function definitions. Perhaps the removal of one of the terms if they are interchangeable. (I noticed that the description for listing 3-1 describes a "declaration" and then is followed immediately with a sentence about "definitions".)

A graphical description of all the statements and expressions and their relationships (statements can be made of expressions etc).

Statement
            |.........................|
                      |........|
fn main() { let y = { let x = 3; x+1 }; }
                            |.| |..|
                    |................|
|.......................................|
Expression

dennismarwood avatar Jan 01 '22 22:01 dennismarwood

Function definitions are statements; you can't say let x = fn main() {};. Function calls are expressions. Hope that clears it up for you right now; I'll think about how to clarify this in the text.

carols10cents avatar Jan 02 '22 02:01 carols10cents

Thank you carols10cents that does it. A voice in the back of my head said that it was something to do with defining vs calling.

I think that the function definitions not ending in a semicolon was throwing me a off bit as well. And if you look at a call to a function it is immediately followed by a semicolon, though that semicolon is the ending of a statement somewhere. Or at least that is what I believe is happening :)

dennismarwood avatar Jan 02 '22 02:01 dennismarwood