Why does Rust not allow slice as a value?

Viewed 280

Why does Rust does not allow creating a slice sa as a value {ptr, len} on the stack and only allows to create rsa as a references to some (anonymous?) value called a fat pointer?

// a is five i32 values on the stack
let a = [1, 2, 3, 4, 5];

// rsa is a reference to (anonymous?) {ptr,len} on the stack
let rsa = &a[..];

// sa would be an explicit struct {ptr,len} on the stack
let sa = a[..];
error[E0277]: the size for values of type `[{integer}]` cannot be known at compilation time
 --> src/main.rs:9:9
  |
9 |     let sa = a[..];
  |         ^^   ----- help: consider borrowing here: `&a[..]`
  |         |
  |         doesn't have a size known at compile-time
  |
  = help: the trait `std::marker::Sized` is not implemented for `[{integer}]`
  = note: to learn more, visit <https://doc.rust-lang.org/book/ch19-04-advanced-types.html#dynamically-sized-types-and-the-sized-trait>
  = note: all local variables must have a statically known size
  = help: unsized locals are gated as an unstable feature
0 Answers
Related