Skip to content
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

Move semantics example on front page of book is problematic #25918

Closed
tomjakubowski opened this issue May 31, 2015 · 1 comment
Closed

Move semantics example on front page of book is problematic #25918

tomjakubowski opened this issue May 31, 2015 · 1 comment

Comments

@tomjakubowski
Copy link
Contributor

http://doc.rust-lang.org/nightly/book/

So how do we solve this problem? There are two approaches we can take. The first is making a copy rather than using a reference:

fn main() {
    let mut x = vec!["Hello", "world"];

    let y = x[0].clone();

    x.push("foo");
}

Rust has move semantics by default, so if we want to make a copy of some data, we call the clone() method. In this example, y is no longer a reference to the vector stored in x, but a copy of its first element, "Hello". Now that we don’t have a reference, our push() works just fine.

The type of x is Vec<&'static str>. It's not necessary to clone anything here, because reference types are Copy. Even calling x[0].clone() is just copying the pointer into y, not making a copy of the string.

@Gankra Gankra added the A-docs label May 31, 2015
@steveklabnik
Copy link
Member

Ahhh bummer. :(

I mean, good for the code, but terrible for me.

steveklabnik added a commit to steveklabnik/rust that referenced this issue Nov 5, 2015
Originally, this was my 30 minute introduction, and we eventually made
it the opener to the book. But as rust-lang#25918 has shown, the example I use
here has some issues. The good news is that Rust makes heap allocation
syntatically expensive, but the bad news is that that means showing
equivalent programs from Rust and other languages is difficult. After
thinking about it, I'm not sure this section is pulling its weight, and
since it has problems, I'd rather just pull it than try to re-write it
right now. I think the book is fine without it.

FIxes rust-lang#25918
steveklabnik added a commit to steveklabnik/rust that referenced this issue Nov 5, 2015
Originally, this was my 30 minute introduction, and we eventually made
it the opener to the book. But as rust-lang#25918 has shown, the example I use
here has some issues. The good news is that Rust makes heap allocation
syntatically expensive, but the bad news is that that means showing
equivalent programs from Rust and other languages is difficult. After
thinking about it, I'm not sure this section is pulling its weight, and
since it has problems, I'd rather just pull it than try to re-write it
right now. I think the book is fine without it.

FIxes rust-lang#25918
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

3 participants