Created
March 12, 2020 15:21
-
-
Save platy/7ba8781da614a28a20aea5a72b0d87cc to your computer and use it in GitHub Desktop.
Remove the [mut] and it compiles, what is the difference?
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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