study rust

RMAG news

1.
Vector is an array it’s length can be expended。

//empty Vector
let mut a = Vec::new();
// init with one element at least
let mut a1= vec![1,2,33,44,55];
// the elements inner vec instace must be same type
enum One {
Ha,
String(String),
Number(u64)
}
let mut a1= vec![One::ha,One::String(String::from(“你好”))];

so how to traverse a1 it’s type is vec;
recommond for in;

enum One {
Ha,
String(String),
Number(u64)
}
let mut a1= vec![One::ha,One::String(String::from(“你好”))];
for item in a1 {

}

print!(“{:#?}”,{a1});

in a1 the control is given to “for”,So after excuted for statement;
a1 was destroied;

you must use &a in order to borrow the reference;like bellow

enum One {
Ha,
String(String),
Number(u64)
}
let mut a1= vec![One::ha,One::String(String::from(“你好”))];
for item in &a1 {

}

print!(“{:#?}”,{a1});

for statement
any Type that implement Iterator trait can be traverse by for;
if the value was traverse is not a borrow refrence; like this

let a = String.from(“dasd”);

for item in a{

}

a will try to mover the control to item;So a muse be a mutable;

let mut a = String.from(“dasd”);

for item in a{

}

Please follow and like us:
Pin Share