相关疑难解决方法(0)

哪种类型对方法的`self`参数有效?

我想创建一个仅在self参数为的情况下工作的方法Rc.我看到我可以使用Box,所以我想我可能会尝试模仿它是如何工作的:

use std::rc::Rc;
use std::sync::Arc;

struct Bar;

impl Bar {
    fn consuming(self) {}
    fn reference(&self) {}
    fn mutable_reference(&mut self) {}
    fn boxed(self: Box<Bar>) {}
    fn ref_count(self: Rc<Bar>) {}
    fn atomic_ref_count(self: Arc<Bar>) {}
}

fn main() {}
Run Code Online (Sandbox Code Playgroud)

产生这些错误:

error[E0308]: mismatched method receiver
  --> a.rs:11:18
   |
11 |     fn ref_count(self: Rc<Bar>) {}
   |                  ^^^^ expected struct `Bar`, found struct `std::rc::Rc`
   |
   = note: expected type `Bar`
   = note:    found type `std::rc::Rc<Bar>`

error[E0308]: mismatched method receiver
  --> …
Run Code Online (Sandbox Code Playgroud)

methods syntax rust

13
推荐指数
1
解决办法
2866
查看次数

标签 统计

methods ×1

rust ×1

syntax ×1