Necessary mutable borrow?

I came across something that I just can't figure out :
I get that this code is rejected by the compiler

trait Borrow {
    fn borrow_mut(&mut self) -> &i32;
    fn borrow_ref(&self) -> &i32;

impl Borrow for i32 {
    fn borrow_mut(&mut self) -> &i32 {

    fn borrow_ref(&self) -> &i32 {

fn main() {
    let mut x: i32 = 5;

    let y1 = x.borrow_mut();
    let y2 = x.borrow_ref();

    println!("{} {}", y1, y2);

But should it be ? Since borrow_mut only returns an immutable reference, it seems like we should be able to borrow x again after it has executed...
More generally, if we have a function of type fn<'a>(x: &'a mut Type1) -> Type2<'a>, where all 'a references in Type2 are immutable, then is it possible to mutate x through Type2 ?

Yes, this is a known limitation of Rust. The behavior that you want would be safe, but there's no way to express it in a Rust function signature currently.

Some past discussion of this issue:

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.