不可以,您不能在函数参数中从self
开始进行分解.
如果你看一下Rust参考文献(associated items引用)中functions的语法,self
左右的部分是唯一处理的(修剪):
Function :
FunctionQualifiers fn IDENTIFIER GenericParams?
( FunctionParameters? )
FunctionReturnType? WhereClause?
( BlockExpression | ; )
...
FunctionParameters :
SelfParam ,?
| (SelfParam ,)? FunctionParam (, FunctionParam)* ,?
SelfParam :
OuterAttribute* ( ShorthandSelf | TypedSelf )
ShorthandSelf :
(& | & Lifetime)? mut? self
TypedSelf :
mut? self : Type
...
在使用self
时,不考虑通过@
进行其他绑定.它需要是identifier pattern或使用类似的语法才能起作用.
您将can从&Self
类型中分离出来,但是由于您仍然不能调用绑定self
,它将被取消作为method receiver的资格,因此与您的foo1()
版本相比,这不会有任何改变.如果希望它在析构某些字段时作为一种方法运行,则需要分两个步骤来完成,如@drewtato's answer中所示.