-
Notifications
You must be signed in to change notification settings - Fork 13.3k
Guide: if #15276
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Guide: if #15276
Conversation
let y = if x == 5 { 10 } else { 15 }; | ||
``` | ||
|
||
Note that I've added the type annotation to `y`, to specify explicitly that I |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The code above is missing the type annotation which this refers to?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Whoops! Fixed in c7638ea, thanks!
Not sure if it makes sense to mention somewhere that actually any block of code surrounded by {} is an expression. I.e. you could do this: let a = {
let a_string = "calculating a...";
println!("{}", a_string);
let a_len=a_string.len();
a_len
}; |
@pfalabella I do think it'd be good to mention it, though I wonder if this would make sense after I explain functions. That looks very similar to
|
@steveklabnik to me (but very possibly just to me...) it's easier to remember just that any block is an expression, as it helps me parse what happens in ifs, matches and yes, functions. let a = {println!("assigning"); 42u } // this is rarely useful
// but logically it's the same thing that happens here
let a = if something {println!("assigning"); 42u } else {6u}
// and here
let a = match something {
1..10 => {println!("assigning"); 42u }
_ => 1
}
// and here
fn gimme_the_answer() -> uint {println!("assigning"); 42u } |
Right. But we haven't talked at all about functions and match yet. I would love to show exactly that, but we can't do it here, we have to do it later. |
@steveklabnik I mean that the first concept (again, for me...) is foundational to understanding the others more easily, as they are all, in a way, a logical generalization. So if you want to mention it, it might make sense to mention it before all the others. |
|
||
We expected an integer, but we got `()`. `()` is pronounced 'unit', and is a | ||
special type in Rust's type system. `()` is different than `null` in other | ||
languages, because `()` is distinct from other types. For example, in C, `null` |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
C has NULL
macro, C++ has also nullptr
. But is it necessary to mention about null
here? I would rather expect better explanation what ()
is and what it has to do with semicolons. The fact that ()
is both name of a type and the only value of that type might be confusing for someone who doesn't know rust so it definitively needs good explanation.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sometimes, people call ()
'nil', hence me explaining it here.
That said, you're right, I can probably expand this a bit. I didn't explicitly say that ;
produces the ()
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed.
@steveklabnik okay, looks good to me apart from my nit noted above on the one sentence about assignment being an expression in Ruby. |
In Rust, however, using `let` to introduce a binding is _not_ an expression: | ||
|
||
```{ignore} | ||
let x = (let y = 5); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
consider adding a comment saying that this is erroneous code.
i get this from the current rustc
:
let x = (let y = 5); // error: found `let` in ident position
but I guess if we put that error output into the example, we'll need one more sentence explaining "The compiler is telling us here that it was expecting to see the beginning of an expression, and a let
can only begin a statement, not an expression."
Sigh. In my rebase I lost a small change, it seems. I have triple-checked that |
Sigh. One more time. |
|
||
This is not the same as this, which won't compile: | ||
|
||
``` |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You need to put the ignore markers here, I think. At least i think this is the source of the current Travis failure.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What I'm currently wondering is: why does my Rustdoc not complain?
Closes #15276 (Guide: if) Closes #15280 (std::os - Add join_paths, make setenv non-utf8 capable) Closes #15314 (Guide: functions) Closes #15327 (Simplify PatIdent to contain an Ident rather than a Path) Closes #15340 (Guide: add mutable binding section) Closes #15342 (Fix ICE with nested macro_rules!-style macros) Closes #15350 (Remove duplicated slash in install script path) Closes #15351 (correct a few spelling mistakes in the tutorial) Closes #15352 (librustc: Have the kind checker check sub-bounds in trait casts.) Closes #15359 (Fix spelling errors.) Closes #15361 (Rename set_broadast() to set_broadcast().) Closes #15366 (Simplify creating a parser from a token tree) Closes #15367 (Add examples for StrVector methods) Closes #15372 (Vec::grow should use reserve_additional, Vec::reserve should check against capacity) Closes #15373 (Fix minor issues in the documentation of libtime.)
Adding the section about if to the guide. I hope this won't conflict with #15229 , but we'll just have to see, I guess.