Created
December 2, 2019 21:32
-
-
Save anru/264698616ceca5b5d5ef1111ba94a5b7 to your computer and use it in GitHub Desktop.
rust
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
struct Foo { | |
string: String, | |
state: i32, | |
} | |
// immutable borring N | |
// mutable - 1 EXCLUSIVE | |
impl Foo { | |
pub fn new(s: String) -> Foo { | |
Foo { | |
string: s, | |
state: 0, | |
} | |
} | |
pub fn bar(&mut self) -> i32 { | |
let s = &self.string[1..3]; | |
self.mutate(s); | |
self.state | |
} | |
pub fn mutate(&mut self, s: &str) { | |
//let s = &self.string[1..3]; | |
self.state += s.len() as i32; | |
} | |
} | |
fn main() { | |
let s = String::from("Capitan Litva"); | |
let mut f = Foo::new(s); // "Capitan Litva"); | |
let result = f.bar(); | |
println!("result = {}", result); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment