From 0958f55155b3a4c93253a667a18f4b6a18fe819a Mon Sep 17 00:00:00 2001 From: Skandesh <42321593+Skandesh@users.noreply.github.com> Date: Mon, 25 Jul 2022 00:06:16 +0530 Subject: [PATCH] Update Vector.md Another way of defining an empty vector --- solutions/collections/Vector.md | 35 ++++++++++++++++++++++++++++++++- 1 file changed, 34 insertions(+), 1 deletion(-) diff --git a/solutions/collections/Vector.md b/solutions/collections/Vector.md index 903405a..bda978d 100644 --- a/solutions/collections/Vector.md +++ b/solutions/collections/Vector.md @@ -28,6 +28,39 @@ fn main() { } fn is_vec(v: &Vec) {} + + +//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 + // 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) {} + ``` 2. @@ -215,4 +248,4 @@ fn main() { ip.display(); } } -``` \ No newline at end of file +```