I have a weird looking piece of code and I understand that Rust compiler rejects it, but I don't understand the specific error message.
TL;DR; Why does Rust rejects this with "doesn't have a size known at compile-time" instead of something like "illegal syntax" or "can't assign a slice to a slice"?
fn main() {
let mut data1 = vec![0, 1, 2, 3].as_slice();
let mut data2 = vec![8, 9].as_slice();
data1[1..3] = *data2; // of course this is illegal; but I don't understand the error message
}
This is the code. In theory it should replace a sub slice of data1
with the data in slice data2
. (The proper way would be a for loop for example, I know!). But let's have a look at this. Rust compiler says:
error[E0277]: the size for values of type `[{integer}]` cannot be known at compilation time
--> src\main.rs:4:5
|
4 | data1[1..3] = *data2;
| ^^^^^^^^^^^ doesn't have a size known at compile-time
|
= help: the trait `std::marker::Sized` is not implemented for `[{integer}]`
Why is the error at data1[1..3]
, only on the left hand side of the assignment? I expected that Rust compiler tells the error is on the right side of the assignment or even the whole assigment. Something like "can't assign a slice to a slice"
.
But why is Rust telling exactly this message? Why is data1[1..3]
of unknown size in this case? Of course [{integer}]
is not Sized
. But there should be no stack allocation necessary at this point? I'd expect any other error message.