error[E0277]: the size for values of type `[u8]` cannot be known at compilation time
--> src/main.rs:8:5
|
8 | Box::new(*bytes)
| ^^^^^^^^ doesn't have a size known at compile-time
|
= help: the trait `Sized` is not implemented for `[u8]`
= note: all function arguments must have a statically known size
Why do you guys know this? Are there any good learning suggestions? I don't want to be bothered by these simple questions anymore.
still have a question:
I want to insert element 1 into the first position of the type [u8]. Apart from the above, is there any other better way?
Such as other programming languages
& is borrowing in Rust. It sort-of means "I temporarily let you see the data in have, but it will go away". In your case the data is in a variable, and variables get destroyed at the end of their scope (nearest }), so you can't let anyone outside of a function have a peek at content of a variable inside the function — the data will be gone before you return the view into it.
You have to use a type that isn't borrowing the variable, like Box<[u8]> or Vec<u8>. In Rust the ownership difference between &[u8] and Box<[u8]> is very important. In GC languages it doesn't matter whether something is in a variable or not. In Rust it is a semantically meaningful difference.
It's important to note that a type like Box<&[u8]> behaves pretty much exactly like &[u8]. That the outer smart-pointer is owned doesn't change the fact that you have a borrowed type inside it. You just have ownership of a borrow.