0

I extracted this example from my actual issue:

fn main() {
    let vec = vec![2u8, 4, 8];
    let vec_ = vec.as_slice();
    let y = Box::new(*vec_);
    println!("{:?}", y);
}

My intention is to transform &[u8] to [u8]. I tried to dereference it by * but I got error:

error[E0277]: the size for values of type `[u8]` cannot be known at compilation time
 --> src/main.rs:4:22
  |
4 |     let y = Box::new(*vec_);
  |                      ^^^^^ doesn't have a size known at compile-time
  |
  = help: the trait `std::marker::Sized` is not implemented for `[u8]`
  = note: to learn more, visit <https://doc.rust-lang.org/book/ch19-04-advanced-types.html#dynamically-sized-types-and-the-sized-trait>
  = note: required by `std::boxed::Box::<T>::new`

error[E0277]: the size for values of type `[u8]` cannot be known at compilation time
 --> src/main.rs:4:13
  |
4 |     let y = Box::new(*vec_);
  |             ^^^^^^^^ doesn't have a size known at compile-time
  |
  = help: the trait `std::marker::Sized` is not implemented for `[u8]`
  = 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 function arguments must have a statically known size
  = help: unsized locals are gated as an unstable feature

Although the compiler give noted link. I read it carefully but cant get useful information to help me to solve my question: how to transform a &[u8] to [u8]?

Shepmaster
  • 388,571
  • 95
  • 1,107
  • 1,366
June Wenston
  • 124
  • 10

0 Answers0