-
-
Save ircnelson/acaa72d3c08989662a772610266651f9 to your computer and use it in GitHub Desktop.
Rust design struct
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
#[derive(Debug)] | |
struct Person { | |
name: String, | |
never_change_this: i32, | |
} | |
impl Person { | |
fn new(name: String, never_change_this: i32) -> Person { | |
Person { | |
name: name, | |
never_change_this: never_change_this, | |
} | |
} | |
fn name(&self) -> &str { | |
&self.name | |
} | |
fn name_mut(&mut self) -> &mut String { | |
&mut self.name | |
} | |
fn never_change_this(&self) -> &i32 { | |
&self.never_change_this | |
} | |
} | |
fn main() { | |
let mut person = Person::new(String::from("Eddy"), 12); | |
println!("After Construction\n{:#?}", person); | |
*person.name_mut() = String::from("Bob"); | |
println!("After Name Change\n{:#?}", person); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment