"expected item, found let" 是什么意思吝啬的? [英] What does "expected item, found let" mean?

查看:155
本文介绍了"expected item, found let" 是什么意思吝啬的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的代码

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] };

返回错误

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

这是什么意思?

推荐答案

这段代码有很多问题,但你得到的错误是因为你试图执行代码,但不是从在函数内:

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] };

你需要把它放在某处.在这里,我已将其添加到 main:

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] };
}

我还必须:

  • 修复向量构建 (vec!)
  • 删除不存在的特征的使用(Storage)
  • 改变getself的类型
  • = 更改为 :
  • 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.

这篇关于"expected item, found let" 是什么意思吝啬的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆