Skip to content

Instantly share code, notes, and snippets.

@anru
Created December 2, 2019 21:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anru/264698616ceca5b5d5ef1111ba94a5b7 to your computer and use it in GitHub Desktop.
Save anru/264698616ceca5b5d5ef1111ba94a5b7 to your computer and use it in GitHub Desktop.
rust
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