Skip to content

Instantly share code, notes, and snippets.

@rust-play
Created March 16, 2022 17:40
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 rust-play/38928f63a53570cdbe184fa6c971c911 to your computer and use it in GitHub Desktop.
Save rust-play/38928f63a53570cdbe184fa6c971c911 to your computer and use it in GitHub Desktop.
Code shared from the Rust Playground
#[derive( PartialEq, Debug, Clone, Default)]
pub struct Thing {
name: String,
number: i32,
}
impl Thing {
fn new(name: &str, number: Option<i32>) -> Thing {
Thing {
name: name.to_string(),
number: number.unwrap_or_default(),
}
}
}
fn main()
{
let mut thing_vector = vec![];
thing_vector.push(Thing::new("John Doe", Some(37)));
thing_vector.push(Thing::new("Jill Doe", None));
for pl in &mut thing_vector {
println!("{} {}", pl.name, pl.number);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment