I'm trying to pass an array to a function:
fn my_func(xs: [usize]) -> usize {
0
}
fn main() {
let arr = [329, 457, 657];
let res = my_func(inp);
}
I get the error:
error[E0277]: the trait bound `[usize]: std::marker::Sized` is not satisfied
--> src/main.rs:1:12
|
1 | fn my_func(xs: [usize]) -> usize {
| ^^ `[usize]` does not have a constant size known at compile-time
|
= help: the trait `std::marker::Sized` is not implemented for `[usize]`
= note: all local variables must have a statically known size
I know about these other questions but they don't seem to apply to my simple situation.
How can I fix the error?
This is not an array, it's a slice; that's the problem. Read What is the difference between a slice and an array?
As the other answers mention, there are several potential avenues to solve the problem, depending on your goals:
[T]
) don't have a size[T; N]
) have a size&[T]
) have a sizeBox<[T]>
) have a size&[T; N]
) have a sizeand so on.
Your problem (and the reason why a
&
fixes your problem) is, that a slice has no known size at compile time.There are several solutions:
Use an explicit length
Using an explicit length will tell the compiler how large the array is and can now decide how much space to reserve for the array.
Use a reference
A reference points to the slice (a fat pointer in fact), which size is known at compile time (depends on your architecture, but typically 32/64 bit).
Use heap allocation
A box is a heap allocated element (in fact a pointer), so that size is known as well.
There are other container (
Rc
,Arc
, ...) which accept a unsized element. You can spot them in the source code easily, because they have a requirement?Sized
for their template argument (seeBox
example).