Why doesn't a mutable borrow of self change to immutable?

前端 未结 2 722
孤城傲影
孤城傲影 2020-12-03 15:03

This code fails the dreaded borrow checker (playground):

struct Data {
    a: i32,
    b: i32,
    c: i32,
}

impl Data {
    fn reference_to_a(&mut self         


        
2条回答
  •  庸人自扰
    2020-12-03 15:20

    Lifetimes are separate from whether a reference is mutable or not. Working through the code:

    fn reference_to_a(&mut self) -> &i32
    

    Although the lifetimes have been elided, this is equivalent to:

    fn reference_to_a<'a>(&'a mut self) -> &'a i32
    

    i.e. the input and output lifetimes are the same. That's the only way to assign lifetimes to a function like this (unless it returned an &'static reference to global data), since you can't make up the output lifetime from nothing.

    That means that if you keep the return value alive by saving it in a variable, you're keeping the &mut self alive too.

    Another way of thinking about it is that the &i32 is a sub-borrow of &mut self, so is only valid until that expires.

    As @aSpex points out, this is covered in the nomicon.

提交回复
热议问题