cargo build期间,我收到以下错误.我不确定该如何解决这件事.

error: not &self method
  --> src/new_service.rs:20:18
   |
20 |     pub async fn new() -> Self {
   |                  ^^^

Src/new_service.rs的相关部分:

pub struct Service {
    start: Event,
    collections: Vec<Collection>,
    clients: Vec<Client>,
}

#[dbus_interface(name ="org.freedesktop.Secret.Service")]
impl Service {
    pub async fn new() -> Self {
        Service {
            start: event_listener::Event::new(),
            collections: Vec::new(),
            clients: Vec::new(),
        }
    }
.....

推荐答案

对于同一类型,您可以有多个impl块,这在这里看起来是必要的,因为new不是可调用的DBus方法.试着这样组织您的代码:

pub struct Service {
    start: Event,
    collections: Vec<Collection>,
    clients: Vec<Client>,
}

impl Service {
    pub fn new() -> Self {
        Service {
            start: event_listener::Event::new(),
            collections: Vec::new(),
            clients: Vec::new(),
        }
    }
}

#[dbus_interface(name ="org.freedesktop.Secret.Service")]
impl Service {
    .....
}

Rust相关问答推荐

为什么幻影数据不能自动推断?

值为可变对象的不可变HashMap

获取字符串切片(&;str)上的切片([ia..ib])返回字符串

通过不同的字段进行散列和排序的 struct (需要不同的EQ实现)

在析构赋值中使用一些现有绑定

如何模拟/创建ReqData以测试Actix Web请求处理程序?

如何高效地将 struct 向量中的字段收集到单独的数组中

循环访问枚举中的不同集合

Rust将String上的迭代器转换为&;[&;str]

为什么AsyncRead在Box上的实现有一个Unpin特征绑定?

为什么 tokio 在以奇怪的方式调用时只运行 n 个任务中的 n-1 个?

如何从宏调用闭包?

str 和 String 的 Rust 生命周期

如何从 rust 中的同一父目录导入文件

为什么允许重新分配 String 而不是 *&String

为什么拥有 i32 所有权的函数需要它是可变的?

`use std::error::Error` 声明中断编译

你能告诉我如何在 Rust 中使用定时器吗?

匹配结果时的简洁日志(log)记录

为什么在使用 self 时会消耗 struct 而在解构时不会?