Skip to content

Instantly share code, notes, and snippets.

@Shaun289
Created November 4, 2021 04:31
Show Gist options
  • Save Shaun289/0fa3b3888b769675b7e889c30053384c to your computer and use it in GitHub Desktop.
Save Shaun289/0fa3b3888b769675b7e889c30053384c to your computer and use it in GitHub Desktop.
Rust study : match pointer/reference
/*
The rust by example ko https://hanbum.gitbooks.io/rustbyexample/content/flow_control/match/destructuring/destructure_pointers.html
compiled on https://play.rust-lang.org/
result :
Got a value via destructuring 4
Got a value via dereferencing 4
Got a reference to a value 5
We added 10. `mut_value`: 16
mut_value after match : 16
*/
fn main()
{
let reference = &4;
match reference {
&val => println!("Got a value via destructuring {:?}", val),
}
match *reference {
val => println!("Got a value via dereferencing {:?}", val),
}
let _not_a_reference = 3;
let ref _is_a_reference = 3;
let value = 5;
let mut mut_value = 6;
match value {
ref r => println!("Got a reference to a value {:?}", r),
}
match mut_value {
ref mut m => {
*m += 10;
println!("We added 10. `mut_value`: {:?}", m);
},
}
println!("mut_value after match : {}", mut_value)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment