Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Rust: why does borrowing a reference inside struct borrow the whole struct?

I have the following code:

struct MyStruct<'a>{
    data: &'a str,
}

fn get<'a>(S: &'a MyStruct<'a>) -> &'a str{
    S.data
}

fn set<'a>(S: &'a mut MyStruct<'a>, x: &'a str){
    S.data = x;
}

fn main(){
    let mut S = MyStruct{data: "hello"};
    let foo: &str = get(&S);
    set(&mut S, "goodbye");
    dbg!(foo);
}

This fails to compile because let bar: &str = get(&S) takes an immutable borrow of S, and on the next line we take a mutable borrow. But we didn’t borrow the whole Struct S, just the reference inside the struct. Why is the borrow still active?

I think it has something to do with the lifetime annotations in get and set. Those functions are my attempt to "desugar" how the corresponding member functions would look like. If I change the signature of get to fn get<'a, 'b>(S: &'a MyStruct<'b>) -> &'b str, the code compiles. Why does the signature affect the duration of the borrow?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

By specifying the lifetime in get() to be fn(&'a MyStruct<'a>) -> &'a str, you say you’re borrowing the whole struct for the lifetime of the string. Because the string is used after the set(), that time period includes the set(). Thus, during the set() the struct is borrowed, which is an error.

If, on the other hand, you specify it to be fn(&'b MyStruct<'a>) -> &'a str, you borrow the struct only for the get(), and return the string inside with a different lifetime.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading