Soliciting multiple new examples for rust-lang.org homepage


#16

Just C++ would probably be a mistake, but a variety of languages might be interesting. Then again, I think not defining Rust in terms of other languages is a good point.


#17

The homepage should present Rust, in terms of what Rust can do and what Rust prevents. I have nothing against detailed comparison of Rust to other languages, but it should be somewhere else.


#18

I think, macros are also one of those features, which is nice of Rust.
Maybe, systems programmers are used to have it, but someone from a Ruby/Java/Python/… background has probably not. Web-people will have experience with preprocessors, but macros are a bit more than just string replacements.

I just don’t have a handy example… this one feels more like a job for impl Person http://is.gd/4u8Ptl :slight_smile:


#19

I don’t think this is particularly apropos, but what can I say, I love stack machines. :stuck_out_tongue:

// This code is editable and runnable!
fn main() {
    let program = "1 2 3 swap rot * + 4 swap - -2 /";
    let mut stack: Vec<i64> = vec![];
    macro_rules! pop { () => {stack.pop().expect("stack empty")} }
    macro_rules! push { ($($e:expr),*) => {{$(stack.push($e);)*}} }
    for word in program.split_whitespace() {
        match word {
            "+" => { let v = pop!() + pop!(); stack.push(v); }
            "-" => { let v = pop!() - pop!(); stack.push(v); }
            "*" => { let v = pop!() * pop!(); stack.push(v); }
            "/" => { let v = pop!() / pop!(); stack.push(v); }
            "swap" => { let (b,a) = (pop!(),pop!()); push!(a,b) }
            "rot" => {
                let (c,b,a) = (pop!(),pop!(),pop!());
                push!(b,c,a);
            }
            other => stack.push(other.parse().expect("not integer"))
        }
    }
    println!("result: {}", pop!());
}

Also, it’s really hard to fit anything interesting into the existing space: 69 wide, 21 high.


#20

How about an example showing how to use the Entry api in HashMaps. I always feel that mos of these simple examples fail to show why it is useful and what it can provide. The Entry api shows how rust’s constraints can allow a very nice, safe and fast api that is not really possible in most (any?) other languages. In addition, I think it shows that the rust standard library is not bad in terms of apis. The example should, as was suggested here, have comments explaining how the Entry object means that the HashMap can’t be modified, etc…


#21

Yehuda’s work for fast_blank in rust is one of my favorite examples for zero cost abstractions + C FFI. @steveklabnik has a nice tldr on reddit. It’s also about 100x shorter than the matching C implementation.


#22

There’s an error. Your solidus invokes the multiplly operation instead of the division one.


#23

No it doesn’t, don’t be ridiculous. cough


#24

I like their site too, though it’s focus is a lot different from ours. I like that they have small examples, paired with explanations. I don’t love that there’s so much text visible by default.

I see ours primarily directing you to how to install then how to find more information. Assuming people know Rust by reputation and don’t need to be sold again, but giving you a quick hint that you are in the right place, get you excited, then links to more info. I want to preserve our minimalism (on this page at least).

What I’d like to see is several small examples, that we can (when JS exists) display one at a time. Possibly each paired with a one-sentence description and a link to more information.


#25

On the subject of Cargo, I’d like to be able to show and run Cargo examples. Play can be fixed to support it perhaps. I don’t want to explain Cargo.toml on the Rust home page. Having to explain that the example doesn’t work without pairing it with another file, and one that is not interesting or relevant to demonstrate the beauty of the Rust language, is extremely undesirable to me. Cargo should have a subcommand or plugin that will figure out what crates you want from the source so we can say 'Run this as main.rs locally with cargo blah'.

cc @Lurkin about play not dealing with Cargo

Edit: cargo-script does something close to what I’d expect for demoing simple cargo-ified source. The only annotation in requires is // cargo-deps.


#26

cargo-script bundles together fetching, building and execution, which may or may not be an issue with the playpen. Just FYI: you can use --gen-pkg-only to have it “extract” the source file into a regular Cargo package. If that’s too heavy-weight, I could also always just spin the relevant code into an external crate.


#27

There might be some nice examples on https://github.com/Hoverbear/rust-rosetta if anyone needs ideas.


#28

Discussing with Lurkin on irc, the current plan for external crates on playpen is basically a whitelist. So it should be pretty easy to just drop the whitelisted crates in cargo’s cache, configure it to have a bogus index URL, and call it a day.


#29

Here’s a “doesn’t compile, thank goodness” example:

fn append_slice<T: Copy>(v: &mut Vec<T>, s: &[T])
{
    for &t in s { v.push(t); }
}

fn main() {
    let mut v = vec![1,2,3];
    let w = vec![4,5,6];
    
    append_slice(&mut v, &w[..]); // This is okay.
    assert_eq!(v, [1,2,3,4,5,6]);
   
    // This doesn't compile, and that's a good thing:
    // what if we reallocated v's buffer mid-append?
    // s would be pointing at freed memory!
    append_slice(&mut v, &v[..]);
}

#30

Here is a Gist containing various examples. I tried to get at least one for each “bullet point” feature on the front page. All of them fit within the existing 69×22 cell space (which was a bastard to do, let me tell you).

The only one that requires a Cargo dep is twdr-crossbeam.rs that shows mutating an array on the stack with multiple threads. That (and all the others, incidentally) can be run directly with cargo script.


#31

Do we guarantee left-to-right evaluation order?


#32

I can understand the idea of replacing that packed code with some more simple examples. But I’ve to say something against it, it probably won’t change anything, as I can see both sides, anyway:

The part that gave me the kick, to learn rust, after reading all the stuff like guaranteed memory safety, was exactly this, stuffed, code.
Why ?
Because it showed, that rust is able to work with things like strings & patterns as a low-level lang without much extra work. It also showed how easy it’s to directly print the result, in a formatted, good way.
It was the thing that hit me.

Of course you can also show this by dividing it into multiple examples, but for the “fast reader”, like I can be one, it was the best introduction.


#33

Please add more examples which are friendlier to new users

  1. after hello world and data types

    use std::io;
    
    fn main() {
        let mut user_input = String::new();
        io::stdin().read_line(&mut user_input)
            .expect("Failed to read line!");
    
        println!("Hello, World.\n{}", user_input);
    }
    
  2. after structs and trails

    struct Player {
        first_name: String,
        last_name: String,
    }
    
    impl Player {
        fn new(first_name: String, last_name: String) -> Player {
            Player {
                first_name : first_name,
                last_name : last_name,
            }
        }
        
        fn full_name(&self) -> String {
            format!("{} {}", self.first_name, self.last_name)
        }
    }
    
    fn main() {
        let player_name = Player::new("Serena".to_string(), "Williams".to_string()).full_name();
        println!("Player: {}", player_name);
    }
    

and etc. Also please order docs properly, new users learn about borrow and checker before basics of structs, vectors and etc. This will confuse the absolute new comers.


#34

I think that goal is served by the “more examples” link, to me the goal of the example visible directly at the home page is to show the power of Rust, or “why should I bother with this language” if you will, but the OO model of Rust is sufficiently unfamiliar to C++/Java programmers that I agree it is worth showing up front, if we have multiple examples.

I disagree here, it is important to know the absolute basics of the language to be able to know how and where the borrow checker applies, in other words if you don’t even know how to declare bindings, structs etc. there’s no point in showing you ownership, since the concept is demonstrable only using such constructs.

Also, the concept that you cannot do anything in Rust without understanding the borrow checker is misleading and may drive more users away, if the first thing they’re hit with is this ‘intimidating’ concept of ownership and borrowing, if you will.


#35

This might not be the right place or the right time, but I would like to suggest a new layout for Rust main page.

  1. Top:
    Rust Logo and links for menu items (doc, forum, download, contribute/help, hamburger icon for other menu items)

  2. Next
    Special graphs(random/list with link under the graph for more details) and intro (Rust is a systems programming language that runs blazingly fast, prevents segfaults, and guarantees thread safety.)
    Graphs ex http://thoughtram.io/rust-and-nickel/#/11, graphs in https://medium.com/@robertgrosse/my-experience-rewriting-enjarify-in-rust-723089b406ad#.dcpoie6lo etc

  3. Examples
    Feature list, on click right side code/video getting changed.

  4. Crates and areas uses Rust
    short intro to crates, side crate downloads count & crate count
    Links with/without images for pages like xi editor, hyper, nickel, Piston, arewewebyet and etc.

  5. Users/ Friends of Rust

  6. bottom
    links for fb, youtube, twitter, github, stackoverflow, blog and etc.

Thanks