In this program `foo` is still mutably borrowed?

In this program foo is still mutably borrowed?

#[derive(Debug)]
struct Foo;

impl Foo {
    fn mutate_and_share(&mut self) -> &Self {
        &*self
    }
    fn share(&self) {}
}

fn main() {
    let mut foo = Foo;
    let loan = foo.mutate_and_share();
    foo.share();
    println!("{:?}", loan);
}
error[E0502]: cannot borrow `foo` as immutable because it is also borrowed as mutable
  --> src/main.rs:14:5
   |
13 |     let loan = foo.mutate_and_share();
   |                ---------------------- mutable borrow occurs here
14 |     foo.share();
   |     ^^^^^^^^^^^ immutable borrow occurs here
15 |     println!("{:?}", loan);
   |                      ---- mutable borrow later used here

What is the question?

It doesn't matter the kind of reference you end up with, but the one you start with, in this case a mutable reference. Allowing downgrading borrows would be unsound when combined with methods like RefCell::get_mut

It means giving is important right!
Not returning right?

I wrote a little about this here, although it's easiest to grasp if you already have a feel for references and lifetimes, e.g.

See also misconception #9 (and the last bullet point above is #8).

1 Like

Hi, your question was a bit hard to read, so I've made some tweaks: For future posts, please make sure to check out how code blocks work in this forum. Also, it's good practice to have your question not only in the title, but to have the topic starting post be understandable on its own, too. And for questions regarding programs that don't compile, attaching the compiler error is always a good idea :wink:

2 Likes

This user has been a member of the forum for over two years, and has been asked to format his code snippets in multiple ocasions before. At this point, I'm not really sure if he's just trolling.

1 Like

Well, on the other hand, the top of this topic reminds us they haven't participated in the forum for almost 1 year. One can forget how the syntax works, and the pinned topics are also relatively tricky to find, once you've viewed them once.

Let's appreciate the question did come with a fully working, and easy to understand code example (except for the formatting). Actual trolling seems rather unlikely.

7 Likes

I agree, but I still understand why someone might be frustrated with another user's long-standing unwillingness and/or inability to learn and improve their posts.

I'm often genuinely puzzled by how (supposedly) intelligent adults can think that it's useful to dump a pile of code with no context and a question that's full of errors to the point of being absolutely meaningless. I mean, haven't we got the internet for decades? Anyone who roams around on forums and Q&A sites for a couple of weeks could (and certainly should) notice that there's a significant and very obvious difference between questions that at least make sense and exhibit a minimal effort made by the asker, and those that are entirely useless and impossible to answer. Why people don't get this is beyond my comprehension.

2 Likes