On Github azerupi / fr-rust-presentation
Par Mathieu David / @azerupi
... comparable au C / C++
#include <iostream> #include <vector> int main() { std::vector<int> v; v.push_back(1); int& elem = v[0]; for(int i=2; i<=100; i++) v.push_back(i); std::cout << elem << std::endl; return 0; }
fn main() { let mut v = Vec::new(); v.push(1); let elem = &v[0]; for i in 2..101 { v.push(i); } println!("{}", elem); }
fn exemple() { let mut s = String::from("Hello World!"); } // <-- s sort de sa portée, la mémoire est désallouée
:9:9: 9:10 error: cannot borrow `v` as mutable because it is also borrowed as immutable [E0502] :9 v.push(i); ^ :6:17: 6:18 note: previous borrow of `v` occurs here; the immutable borrow prevents subsequent moves or mutable borrows of `v` until the borrow ends :6 let elem = &v[0]; ^ :13:2: 13:2 note: previous borrow ends here :2 fn main() { ... :13 } ^ error: aborting due to previous error