She*_*ter 13 methods syntax rust
我想创建一个仅在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
--> a.rs:12:25
|
12 | fn atomic_ref_count(self: Arc<Bar>) {}
| ^^^^ expected struct `Bar`, found struct `std::sync::Arc`
|
= note: expected type `Bar`
= note: found type `std::sync::Arc<Bar>`
Run Code Online (Sandbox Code Playgroud)
这是Rust 1.13,您也可以在围栏中看到它
huo*_*uon 15
目前,这些是有效的:
struct Foo;
impl Foo {
fn by_val(self: Foo) {} // a.k.a. by_val(self)
fn by_ref(self: &Foo) {} // a.k.a. by_ref(&self)
fn by_mut_ref(self: &mut Foo) {} // a.k.a. by_mut_ref(&mut self)
fn by_box(self: Box<Foo>) {} // no short form
}
fn main() {}
Run Code Online (Sandbox Code Playgroud)
直到最近,防锈没有这个明确的self形式,只是self,&self,&mut self和~self(老的名称Box).这改变了,只有按值和按引用具有简写内置语法,因为它们是常见的情况,并且具有非常关键的语言属性,而所有智能指针(包括Box)都需要显式形式.
但是,self处理尚未推广以匹配语法,这意味着非Rc指针仍然不起作用.我认为这需要所谓的"特质改革",尚未实施.
目前的讨论正在问题44874中进行.
(值得期待的东西!)