Advertisement
cwchen

[Rust] Borrowing demo

Aug 27th, 2017
505
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Rust 0.29 KB | None | 0 0
  1. fn main() {
  2.     let v = vec![1, 2, 3];
  3.  
  4.     // Borrow v to sum
  5.     let s = sum(& v);
  6.  
  7.     println!("{}", s);
  8.  
  9.     // v is still accessible
  10.     println!("{}", v[1]);
  11. }
  12.  
  13. fn sum(v: &Vec<i32>) -> i32 {
  14.     let mut sum = 0;
  15.  
  16.     for e in v.iter() {
  17.         sum += *e;
  18.     }
  19.  
  20.     sum
  21. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement