Reputation: 469
I want to create a slice of numbers within certain range in Rust. However, the following syntax does not seem to work, as the output is a slice of length 1, composed of a Range
object.
let count = 10;
let slice = [0..count/2];
What's the proper way to create a slice in Rust from a range?
Upvotes: 5
Views: 2934
Reputation: 71605
You cannot create a slice directly from a range (unless you know an upper bound to the number of elements at compile time, in which case you can use an array), you have to use Vec
:
let vec = Vec::from_iter(0..count/2);
let slice = vec.as_slice();
Upvotes: 7