Rust borrowing

let us look at Rust borrowing examples. Borrowing is related to transfer of control of variable or value from one function to another temprorarily.

fn main(){
   let vector = vec![11,21,32];
   show_vector(&vector);
   println!("{}",vector[0]);
}
fn show_vector(x:&Vec<i32>){
   println!("Inside print_vector function {:?}",x);
}

The code above is compiled using the below command.

apples-MacBook-Air:iterators bhagvan.kommadi$ rustc borrowing.rs

The output will be as below:

apples-MacBook-Air:borrowing bhagvan.kommadi$ ./borrowing 
Inside print_vector function [11, 21, 32]
11

Leave a comment