Skip to content

Remove misleading code example from The Guide #15899

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

Merged
merged 1 commit into from
Jul 23, 2014
Merged

Remove misleading code example from The Guide #15899

merged 1 commit into from
Jul 23, 2014

Conversation

aochagavia
Copy link
Contributor

The removed code caused confusion because it is not clear that the type of y is actually ()

@aochagavia
Copy link
Contributor Author

cc @steveklabnik

I think the reader will not even notice we have skipped a piece of text.

@lilyball
Copy link
Contributor

Eh, I think it's a useful thing to point out. It's contrasting variable assignment with a let item. Perhaps you should instead add a sentence to the end pointing out that the value of an assignment is ().

@aochagavia
Copy link
Contributor Author

I think that is a valid alternative. However, at this point in the guide the () type has not been introduced, so it would require to rearrange the section.

EDIT: even if you do this, x = y = 5i is completely useless in real code. Why should we bother people with it?

@lilyball
Copy link
Contributor

@aochagavia I think the point is to contrast a let statement with basically every other type of statement. Assignment is a statement in most languages, but in Rust it's actually an expression. It's not a particularly useful one, but it can in fact be used in a position where an expression is expected.

@steveklabnik
Copy link
Member

I am super torn. @kballard nails it: it's to contrast that let is different than in other languages. However, I also agree with you, @aochagavia, that it's not useful.

So: can we accomplish this goal through some other example? Is there a time when let as expression has a valid use? If not, then I support removing it. If so, I'd support changing the example to that one.

@lilyball
Copy link
Contributor

How about something like this:

Note that assigning to an already-bound variable is still an expression, although the value of the expression is not particularly useful. Unlike C, where an assignment evaluates to the RHS, in Rust the value of an assignment is the unit type () (which we'll talk about later).

No code example necessary. This explanation is largely just to avoid confusion in readers who read the info on let and decide to actually experiment with mere assignment.

@aochagavia
Copy link
Contributor Author

I like it! Do you have any improvements on that @steveklabnik?

EDIT: maybe we can replace "RHS" for something more accessible for beginners.

@steveklabnik
Copy link
Member

Yes, with the expansion of "right hand side," I think that's fine.

@aochagavia
Copy link
Contributor Author

I have committed a slightly modified version. Please tell me if you think something needs to be changed.

@lilyball
Copy link
Contributor

Actually I think "assigned value" instead of "right hand side" may be simpler.

@aochagavia
Copy link
Contributor Author

I think you are right, check out the new version.

The removed code caused confusion because it is not clear that the type of `y` is actually `()`
@steveklabnik
Copy link
Member

Nice.

bors added a commit that referenced this pull request Jul 23, 2014
The removed code caused confusion because it is not clear that the type of `y` is actually `()`
@bors bors closed this Jul 23, 2014
@bors bors merged commit 2d1b87c into rust-lang:master Jul 23, 2014
@aochagavia aochagavia deleted the guide branch October 13, 2014 18:16
bors added a commit to rust-lang-ci/rust that referenced this pull request Nov 16, 2023
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.

4 participants