美文网首页OtherRust
[Rust] Rust Ownership

[Rust] Rust Ownership

作者: 何幻 | 来源:发表于2016-03-05 08:11 被阅读4次

resource scope是owner标识符的lexical scope
borrower scope是该resource的borrower标识符的lexical scope
borrow scope是该resource所有borrower scope的并集
borrow scope不能大于resource scope,避免访问被释放的resource

owner跳出lexical scope会销毁资源

赋值操作,如果实现了Copy trait,则复制,否则move
&,就说明发生了borrow

move转移owner,borrow不转移owner
move后,不能再访问
borrow后,在borrower跳出它的scope前不能访问
borrow后,可以hand over给其他borrower

函数为了返回borrow,必须显式指明所返回borrower的scope与哪个参数borrower的scope一致

相关文章

网友评论

    本文标题:[Rust] Rust Ownership

    本文链接:https://www.haomeiwen.com/subject/ofynkttx.html