0
votes

I'm trying to compile the following code, from the Rust book at the Rust official website.

fn takes_slice(slice: &str) {
    println!("Got: {}", slice);
}

fn main() {
    let s = "Hello".to_string();
    takes_slice(&s);
}

At compilation, it throws the following error

/devl/rust/bc_09/src/main.rs:7:17: 7:19 error: mismatched types: expected &str, found &collections::string::String (expected str, found struct collections::string::String)

/devl/rust/bc_09/src/main.rs:7 takes_slice(&s); ^~ error: aborting due to previous error Could not compile hello_world.

Here is the Rust version I'm running: rustc 1.0.0-nightly (44a287e6e 2015-01-08 17:03:40 -0800)

1

1 Answers

6
votes

That's a really old version of the nightly in Rust terms! Old enough that the &String -> &str coercion isn't available. You just need to upgrade to a newer version.