Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does "expected item, found let" mean?

Tags:

rust

My code

pub struct MyStorage {
    name: Vec<u8>,
}

impl Storage for MyStorage {
    //let mut name: Vec<u8> = [0x11];
    fn get(&mut self) -> Vec<u8> {
        self.name
    }
}

let my_storage = MyStorage { name = [0x11] };

returns the error

error: expected item, found keyword `let`
  --> src/lib.rs:12:1
   |
12 | let my_storage = MyStorage { name = [0x11] };
   | ^^^ expected item

What does that mean?

like image 251
Lexka Avatar asked Mar 11 '15 17:03

Lexka


1 Answers

There's a number of issues with this code, but the error you are getting is because you are trying to execute code but not from within a function:

let my_storage = MyStorage { name = [0x11] };

You need to put that in something. Here, I've added it to main:

pub struct MyStorage {
    name: Vec<u8>,
}

impl MyStorage {
    fn get(self) -> Vec<u8> {
        self.name
    }
}

fn main() {
    let my_storage = MyStorage { name: vec![0x11] };
}

I also had to:

  • fix the vector construction (vec!)
  • remove the usage of a trait that doesn't exist (Storage)
  • change the type of self in get
  • change from = to :

With all that, the code compiles.

like image 53
Shepmaster Avatar answered Sep 18 '22 17:09

Shepmaster