Skip to content
This repository has been archived by the owner on Sep 30, 2020. It is now read-only.

Fix Github ribbon and add new highlighted code example #1

Merged
merged 3 commits into from
Sep 20, 2012
Merged

Fix Github ribbon and add new highlighted code example #1

merged 3 commits into from
Sep 20, 2012

Conversation

bstrie
Copy link
Contributor

@bstrie bstrie commented Sep 20, 2012

No description provided.

By changing `n` and `e` to `name` and `num` it makes it clearer what
the bars are doing in each of the `for` loops and also makes it more
obvious that those values are being closed over for use in the innermost
print statement.
@bstrie
Copy link
Contributor Author

bstrie commented Sep 20, 2012

Note the commit message in that third commit. I feel that making those variable names less cryptic is a major win for first-time readers.

brson added a commit that referenced this pull request Sep 20, 2012
Fix Github ribbon and add new highlighted code example
@brson brson merged commit 308168a into rust-lang:gh-pages Sep 20, 2012
@brson
Copy link
Contributor

brson commented Sep 20, 2012

Fantastic

@nikomatsakis
Copy link
Contributor

It looks great. The only caveat is that post-demoding it has to look a bit different:

fn main() {
    for ["Alice", "Bob", "Carol"].each |name| {
        let name = *name;
        do task::spawn {
            let v = rand::Rng().shuffle([1, 2, 3]);
            for v.each |num| {
                io::print(fmt!("%s says: '%d'\n", name, *num))
            }
        }
    }
}

This is because each gives an (explicit) pointer to the items in the vector now.

@nikomatsakis
Copy link
Contributor

It occurs to me, as a random thought, that if we supported infallible pattern matching in the parameter list, one could write the following:

fn main() {
    for ["Alice", "Bob", "Carol"].each |&name| {
        do task::spawn {
            let v = rand::Rng().shuffle([1, 2, 3]);
            for v.each |&num| {
                io::print(fmt!("%s says: '%d'\n", name, num))
            }
        }
    }
}

which is cool, although maybe it doesn't quite look like what it means. Here the &name and &num are patterns, so they would effectively dereference name and num directly.

@bstrie
Copy link
Contributor Author

bstrie commented Sep 20, 2012

Will the current code example be valid for 0.4?

@bstrie
Copy link
Contributor Author

bstrie commented Sep 20, 2012

Also that second revised code example looks fine (a little hesitant to start using the sigils in the introductory code example, but not that bad), but that first revised example looks bad enough that I'd want to just rewrite the code example entirely.

@brson
Copy link
Contributor

brson commented Sep 20, 2012

@bstrie. No, it won't be valid for 0.4.

@bstrie
Copy link
Contributor Author

bstrie commented Sep 20, 2012

@nikomatsakis I don't suppose that infallible-matching-in-parameter-lists is so uncontroversial and easily implemented that it could arrive in time for 0.4, is it?

@nikomatsakis
Copy link
Contributor

@bstrie uncontroversial, yes, easily implemented, no. It's actually not that hard...but it certainly wasn't on our priority list.

One other comment about the example: the old version relied on an implicit copy of a ~str, which i believe generates a warning (or should). It'd probably be nice to have the opening example be warning free.

brson pushed a commit that referenced this pull request Sep 10, 2015
brson pushed a commit that referenced this pull request Oct 29, 2015
nikomatsakis referenced this pull request in nikomatsakis/rust-www Jan 5, 2016
steveklabnik pushed a commit that referenced this pull request Oct 10, 2016
alexcrichton pushed a commit that referenced this pull request Feb 14, 2017
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants