Why is `&mut &foo` valid but `&mut a_ref_to_foo` is invalid?

Viewed 81

In the following example, t1 compiles but t2 does not.

Is there anything special about &mut &stream? I don't think Deref kicks in.

use std::net::TcpStream;

fn t1() {
    let stream = TcpStream::connect("127.0.0.1").unwrap();
    let a = &mut &stream;
}

fn t2(stream: &TcpStream) {
    let a = &mut stream;
}

Playground

9  | fn t2(stream: &TcpStream) {
   |       ------ use `mut stream` here to make mutable
10 |     let a = &mut stream;
   |                  ^^^^^^ cannot borrow mutably
1 Answers
Related