To get a sub-array in Rust, you can use slicing on arrays or vectors.
In this example,
numbers
with some values.fn main() {
let numbers = [1, 2, 3, 4, 5];
let sub_array = &numbers[2..=4];
println!("Sub-array: {:?}", sub_array);
}
Sub-array: [3, 4, 5]
In this example,
characters
with some values.fn main() {
let characters = ['a', 'b', 'c', 'd', 'e'];
let sub_array = &characters[1..=3];
println!("Sub-array: {:?}", sub_array);
}
Sub-array: ['b', 'c', 'd']
In this tutorial, we learned How to get Sub Array in Rust language with well detailed examples.