Skip to content

Instantly share code, notes, and snippets.

@fatihgokce
Created August 8, 2019 10:46
Show Gist options
  • Save fatihgokce/74503322d5113a5e944c3119b0b33ace to your computer and use it in GitHub Desktop.
Save fatihgokce/74503322d5113a5e944c3119b0b33ace to your computer and use it in GitHub Desktop.
fn main() {
let mut val1 = 2;
val1 = 3; // OK
let val2 = 2;
//val2 = 3; // error: re-assignment of immutable variable
let val1 = &mut 2;
*val1 = 3; // OK
println!("Hello, world!");
let box1=Box::new(3);
let b=modify_foo(box1);
println!("{}",b);
let mut r=3;
let b2=modify_foo2(&mut r);
println!("{}",b2);
let mut b2=Box::new(5);
*b2=6;
let mut s = String::from("hello");
change(&mut s);
println!("{}",s);
}
fn change(some_string: &mut String) {
some_string.push_str(", world");
}
fn modify_foo(mut foo: Box<i32>)->i32 { *foo += 1; *foo }
fn modify_foo2(foo: &mut i32)->i32 { *foo += 1; *foo }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment