fn main(){ let a = 123; let p = &a; println!("{}", *p); let b = 456; let ref q = &b; println!("{}", *q); let c = 789; let ref r = &c; println!("{}", *r); }
#include <cstdio> void swap(int &a, int &b) { int temp;; temp = a; a = b; b = temp; } void swap_pointer(int *a, int *b){ int temp; temp = *a; *a = *b; *b = temp; } int main() { int x = 5; int y = 8; swap(x, y); printf("%d, %d\n", x, y); swap_pointer(&x, &y); printf("%d, %d\n", x, y); return(0); }
これをRustで書こうとするが、、、
fn swap<'a>(mut a: &'a i32,mut b: &'a i32) { let mut _temp:&i32; _temp = a; a = b; b = _temp; } fn main(){ let mut x: i32 = 5; let mut y: i32 = 8; swap(&x, &y); println!("x:{} y:{}", x, y); }
$ ./main
x:5 y:8
swapされんな… 何が間違っているかよくわからん…