Skip to content

Instantly share code, notes, and snippets.

@platy
Created March 12, 2020 15:21
Show Gist options
  • Save platy/7ba8781da614a28a20aea5a72b0d87cc to your computer and use it in GitHub Desktop.
Save platy/7ba8781da614a28a20aea5a72b0d87cc to your computer and use it in GitHub Desktop.
Remove the [mut] and it compiles, what is the difference?
fn main() {
let mut string = String::from("Hello World!");
println!("{}", get_back_the_same_string(string.as_mut_str()));
}
fn get_back_the_same_string<'a>(string: &'a mut str) -> &'a str {
let nt = NewType(string);
return nt.get_string()
}
struct NewType<'b>(&'b mut str);
// ^^^ if this was immutable, it compiles and below self.0 can be returned from the method. if it is mutable, it can't
impl <'b> NewType<'b> {
fn get_string(&self) -> &'b str {
return self.0
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment