let numbers = [1, 1, 2, 3, 5, 8, 13];
let numbers_subset = numbers[0..=3];
The resulting error, which I expected, is:
let numbers_subset = numbers[0..=3];
| ^^^^^^^^^^^^^^ doesn't have a size known at compile-time
help: consider borrowing here
|
116 | let numbers_subset = &numbers[0..=3];
My reasoning was, that shadowing the numbers variable with numbers_subset would make it no longer possible to count elements in numbers.
But in that case I can' explain why the following is possible:
let numbers = [1, 1, 2, 3, 5, 8, 13];
let numbers_subset = numbers[3];
In both cases there is shadowing and there is counting, iterating. So why the difference?
Probably I'm missing something very simple here...
What do you mean by "shadowing"? Shadowing in rust refer to the action of creating a new variable with the same name as an existing variable. In your case there are just two variables, with different names, so by Rust terms there's no shadowing.
As to why one works and the other doesnt, numbers[0..=3] doesn't create an array, but a slice. It's just a view with no statically (i.e. at compile time) size. This is why the compiler tells you "doesn't have a size known at compile-time". numbers[3] however is just a single element, of type i32, whose size is statically known and can thus be held in a variable.
There's no shadowing, counting or iterating involved in any of the two cases.