Update Vector.md

Another way of defining an empty vector
This commit is contained in:
Skandesh 2022-07-25 00:06:16 +05:30 committed by GitHub
parent d91ca79253
commit 0958f55155
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 34 additions and 1 deletions

View File

@ -28,6 +28,39 @@ fn main() {
}
fn is_vec(v: &Vec<u8>) {}
//Another solution
fn main() {
let arr: [u8; 3] = [1, 2, 3];
let v = Vec::from(arr);
is_vec(&v);
let v = vec![1, 2, 3];
is_vec(&v);
// vec!(..) and vec![..] are same macros, so
let v = vec!(1, 2, 3);
is_vec(&v);
// in code below, v is Vec<[u8; 3]> , not Vec<u8>
// USE Vec::new and `for` to rewrite the below code
let mut v1 = vec!();
for i in &v{
v1.push(*i);
}
is_vec(&v1);
assert_eq!(v, v1);
println!("Success!")
}
fn is_vec(v: &Vec<u8>) {}
```
2.
@ -215,4 +248,4 @@ fn main() {
ip.display();
}
}
```
```