Skip to content

Change the example on rust-lang.org #3407

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
brson opened this issue Sep 7, 2012 · 18 comments
Closed

Change the example on rust-lang.org #3407

brson opened this issue Sep 7, 2012 · 18 comments
Assignees
Milestone

Comments

@brson
Copy link
Contributor

brson commented Sep 7, 2012

The current example is too cute - the times method is not well loved. Let's come up with another one. Maybe something that shows more of the language.

@eholk
Copy link
Contributor

eholk commented Sep 7, 2012

Something showing off pattern matching would be cool.

@ghost ghost assigned brson Sep 10, 2012
@bstrie
Copy link
Contributor

bstrie commented Sep 18, 2012

Here's what I proposed on IRC that met with a favorable response:

/* Rust example blurb, with annotated concepts as each is introduced. */
fn main() {
    // basic for loop, optional parentheses, optional argument list
    for 10.times {
        // do notation, module access, task creation
        do task::spawn {
            // type inference, method access, vector literals
            let items = rand::Rng().shuffle([1, 2, 3]);
            // slightly more advanced for loop, macros, format strings, optional semicolons
            for items.each |e| { io::print(fmt!("%d ", e)) }
        }
    }
}

You should probably take out the comments though (especially without syntax highlighting). And of course you can replace 10.times with iter::repeat(10) if you'd like (though I still prefer the look of .times).

@graydon
Copy link
Contributor

graydon commented Sep 18, 2012

Thanks @bstrie, that's pretty good! With a small tweak to the kind rules to admit static region pointers and slices to the Send kind (as they should be), we can even do this:

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

I think that reads pretty well, flexes a fair number of interesting features.

@bstrie
Copy link
Contributor

bstrie commented Sep 19, 2012

Looks good, but how about changing:

io::print(fmt!("%s says: '%d'\n", n, e))

to:

io::println(fmt!("%s says: '%d'", n, e))

@kud1ing
Copy link

kud1ing commented Sep 19, 2012

Nice. I suggest to rename "Rng", though.
Maybe Haskell's "RandomGen" or simply "Generator"?

@bstrie
Copy link
Contributor

bstrie commented Sep 19, 2012

@Lenny222 Generator is a bit vague (especially if someone just imports Generator rather than rand::Generator). RandomGen could work, but personally I don't think that Rng is at all ambiguous.

@graydon
Copy link
Contributor

graydon commented Sep 19, 2012

io::println(fmt!("%s says: '%d'", n, e)) interleaves the string and the following \n when writing. Wind up with some non-spaced and some double-spaced lines as the threads race on stdout.

@bstrie
Copy link
Contributor

bstrie commented Sep 20, 2012

This is done.

@bstrie
Copy link
Contributor

bstrie commented Sep 20, 2012

Actually no, Niko's pointed out that the new code example won't be valid post-demoding. See rust-lang/prev.rust-lang.org#1

@luser
Copy link
Contributor

luser commented Sep 20, 2012

This code sample doesn't compile for me with a rustc built from master. I get:

$ rustc sample.rs
sample.rs:6:43: 6:47 error: not a sendable value
sample.rs:6          io::print(fmt!("%s says: '%d'\n", name, num))
                                                       ^~~~
note: in expansion of #fmt
sample.rs:6:19: 6:54 note: expansion site
error: aborting due to previous error

@graydon
Copy link
Contributor

graydon commented Sep 20, 2012

Yeah, the changes to enable it are still trickling into master from incoming. I'll bump master.

@graydon
Copy link
Contributor

graydon commented Sep 25, 2012

Still doesn't compile, requires more work on kinds and * on the name, num arguments. Or changing again.

@bstrie
Copy link
Contributor

bstrie commented Sep 25, 2012

Because of #3586, I think it makes sense to think of an example that forgoes vec::each entirely. I'd still really like to show off for loops, though.

@brson
Copy link
Contributor Author

brson commented Sep 26, 2012

I updated the example to make it build, but agree we should get the let name = *name; line out of there.

@brson
Copy link
Contributor Author

brson commented Sep 26, 2012

Now i threw in a copy to make that line look more significant: let name = copy *name;

@brson
Copy link
Contributor Author

brson commented Sep 27, 2012

This would work seamlessly using .consume(), which is an efficient, by-value version of each that destroys the vector as it goes. I think this function (and map_consume) will be used more often now that each and map pass arguments by reference. I do think we should remove the uint argument from the consume callback though.

@brson
Copy link
Contributor Author

brson commented Sep 27, 2012

Oh, but maybe by-value self types don't work yet...

@graydon
Copy link
Contributor

graydon commented Oct 4, 2012

This seems done enough to close now. Reopen if you change your mind :)

@graydon graydon closed this as completed Oct 4, 2012
bors pushed a commit to rust-lang-ci/rust that referenced this issue May 15, 2021
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

No branches or pull requests

6 participants