Skip to content

Instantly share code, notes, and snippets.

Created October 29, 2017 05:26
Show Gist options
  • Save anonymous/85251f4206f7334fa84639067b334e52 to your computer and use it in GitHub Desktop.
Save anonymous/85251f4206f7334fa84639067b334e52 to your computer and use it in GitHub Desktop.
Rust code shared from the playground
#[derive(Default, Debug, Clone)]
struct Sth { pub log: Option<Log> }
#[derive(Default, Debug, Clone)]
struct Log { pub access: Option<String> }
fn main() {
let sth = Sth { ..Default::default() };
println!("By default, sth.log.access is not existed, because sth.log is {:?}", sth.log);
let mut sth = Sth { log: Some(Log { access: Some(String::from("yes")) }) };
// let ref_sth = &sth;
println!("Now, `sth.log.access` is {:?}", sth.log.as_ref().unwrap().access.as_ref().unwrap());
// And If you want to change the `sth.log.access` to "No", you should
if let Some(ref mut log) = sth.log {
if let Some(ref mut access) = log.access {
*access = String::from("No");
}
}
println!("Here, `sth.log.access` is {:?}", sth.log.as_ref().unwrap().access.as_ref().unwrap());
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment