Reverse an Array manually in Rust
Reverse an Array
1
2
3
4
5
6
7
8
9
10
fn reverse_array(a: &[i32]) -> Vec<i32> {
let mut number = a.len();
let mut result:Vec<i32> = vec![];
while number != 0{
number = number - 1;
result.push(a[number]);
}
return result;
}
Call above method in main
1
2
3
4
5
6
7
8
9
fn main() {
let array: [i32; 5] = [5, 4, 3, 2, 1];
let result = reverse_array(&array);
for i in 0..result.len() {
println!("{}", result[i]);
}
}
1
2
3
4
5
6
7
8
9
cargo run
## rust compile text
## ....
# Output
1
2
3
4
5
This post is licensed under CC BY 4.0 by the author.