Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Print sliced vector results in "Sized is not satisfied"

Tags:

rust

I am trying to slice a vector and print it simultaneously in Rust. This is my code:

fn main() {
    let a = vec![1, 2, 3, 4];
    println!("{:?}", a[1..2]);
}

Error:

error[E0277]: the trait bound `[{integer}]: std::marker::Sized` is not satisfied
 --> src/main.rs:6:5
  |
6 |     println!("{:?}", a[1..3]);
  |     ^^^^^^^^^^^^^^^^^^^^^^^^^^ trait `[{integer}]: std::marker::Sized` not satisfied
  |
  = note: `[{integer}]` does not have a constant size known at compile-time
  = note: required by `std::fmt::ArgumentV1::new`
  = note: this error originates in a macro outside of the current crate

How do I print this sliced vector?

like image 520
Abhijay Ghildyal Avatar asked Dec 09 '16 12:12

Abhijay Ghildyal


1 Answers

You need to use a reference; it worked for me in Rust 1.13.

println!("{:?}", &a[1..3]);
like image 81
anon8112 Avatar answered Nov 03 '22 23:11

anon8112