Skip to content

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

Closed
wants to merge 1 commit into from
Closed

Guide: if #15276

wants to merge 1 commit into from

Conversation

steveklabnik
Copy link
Member

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.

let y = if x == 5 { 10 } else { 15 };
```

Note that I've added the type annotation to `y`, to specify explicitly that I
Copy link
Contributor

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?

Copy link
Member Author

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!

@pfalabella
Copy link
Contributor

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
    };

@steveklabnik
Copy link
Member Author

@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

let a = foo();

// elsewhere

fn foo() {
    // what you wrote
}

@pfalabella
Copy link
Contributor

@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.
i.e.

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 }

@steveklabnik
Copy link
Member Author

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.

@pfalabella
Copy link
Contributor

@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.
But I won't get offended if you decide the guide flows better without it (great job, by the way!!).


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`
Copy link
Contributor

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.

Copy link
Member Author

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 ().

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed.

@pnkfelix
Copy link
Member

pnkfelix commented Jul 2, 2014

@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);
Copy link
Member

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."

@steveklabnik
Copy link
Member Author

Sigh. In my rebase I lost a small change, it seems. I have triple-checked that rustdoc doesn't give any errors on my machine. r? @pnkfelix

@steveklabnik
Copy link
Member Author

Sigh. One more time.


This is not the same as this, which won't compile:

```
Copy link
Member

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.

Copy link
Member Author

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?

bors added a commit that referenced this pull request Jul 3, 2014
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.)
@bors bors closed this in #15377 Jul 3, 2014
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

8 participants