tutorial: Remove confusing discussion about semicolons
This takes up a lot of words and isn't very clear. The previous discussion gets they idea across. /cc: #4217
This commit is contained in:
parent
9cbc0fd0d9
commit
e0f9f89c65
1 changed files with 0 additions and 40 deletions
|
@ -308,46 +308,6 @@ fn is_four(x: int) -> bool {
|
|||
}
|
||||
~~~~
|
||||
|
||||
If all those things are expressions, you might conclude that you have
|
||||
to add a terminating semicolon after *every* statement, even ones that
|
||||
are not traditionally terminated with a semicolon in C (like `while`).
|
||||
That is not the case, though. Expressions that end in a block only
|
||||
need a semicolon if that block contains a trailing expression. `while`
|
||||
loops do not allow trailing expressions, and `if` statements tend to
|
||||
only have a trailing expression when you want to use their value for
|
||||
something—in which case you'll have embedded it in a bigger statement.
|
||||
|
||||
~~~
|
||||
# fn foo() -> bool { true }
|
||||
# fn bar() -> bool { true }
|
||||
# fn baz() -> bool { true }
|
||||
// `let` is not an expression, so it is semicolon-terminated;
|
||||
let x = foo();
|
||||
|
||||
// When used in statement position, bracy expressions do not
|
||||
// usually need to be semicolon terminated
|
||||
if x {
|
||||
bar();
|
||||
} else {
|
||||
baz();
|
||||
} // No semi-colon
|
||||
|
||||
// Although, if `bar` and `baz` have non-nil return types, and
|
||||
// we try to use them as the tail expressions, rustc will
|
||||
// make us terminate the expression.
|
||||
if x {
|
||||
bar()
|
||||
} else {
|
||||
baz()
|
||||
}; // Semi-colon to ignore non-nil block type
|
||||
|
||||
// An `if` embedded in `let` again requires a semicolon to terminate
|
||||
// the `let` statement
|
||||
let y = if x { foo() } else { bar() };
|
||||
~~~
|
||||
|
||||
This may sound intricate, but it is super-useful and will grow on you.
|
||||
|
||||
## Types
|
||||
|
||||
The basic types include the usual boolean, integral, and floating-point types.
|
||||
|
|
Loading…
Add table
Reference in a new issue