< 返回版块

群问答关于&'a mut self 编译出错的解释

枪炮与玫瑰 发表于

出错示例,使用了 &'a mut self ,应修正为 &mut self

出错play地址

国外论坛详解

if you drop the 'a named lifetime in mutate_internal, what you get is a fresh (anonymous) lifetime parameter, not 'a. I.e. you get something equivalent to:

fn mutate_internal<'b>(&'b mut self) {} this means that self is borrowed until mutate_internal is done, but no longer than that. That's why the second call to mutate_internal compiles.

By contrast, with fn mutate_internal(&'a mut self) {} you're telling the compiler that self will be borrowed as long as 'a (which is the entire lifetime of Foo). That's why the second mutate_internal can't be called

评论区

admin 2018-06-13T03:10:10.211567

赞。

1 共 1 评论, 共 1 页