-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvectors.rs
More file actions
49 lines (43 loc) · 1.1 KB
/
Copy pathvectors.rs
File metadata and controls
49 lines (43 loc) · 1.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
//Vectors
// fn main(){
// let mut vec = Vec::new();
// vec.push(1);
// vec.push(2);
// vec.push(3);
//Intialising using macros
// Explicity specifying the type
// let vec: Vec<i32> = vec![1,2,3];
// println!("{:?}",vec);
// let ans = even_filter(&vec);
// println!("even numbers of vec: {:?}",ans);
// println!("can we use vec again without passing &vec to tthe even_filter? No: {:?}",vec);
// }
//Write a function that takes a vector as input and returns a vevtor with even values
//Approach 1:
// fn even_filter(vec: &Vec<i32>) -> Vec<i32>{
// let mut vec2 = Vec::new();
// for val in vec{
// if val%2 == 0{
// vec2.push(*val);
// }
// }
// return vec2;
// }
//Approach 2
fn main(){
let mut vec: Vec<i32> = vec![1,2,3];
let ans = even_filter2(&mut vec);
println!("only even elements: {:?}",ans);
}
fn even_filter2(v: &mut Vec<i32>) -> Vec<i32>{
let mut i = 0;
while i < v.len(){
if v[i] %2 !=0{
v.remove(i);
}
else{
i += 1;
}
}
return v.to_vec();
}