Scope extended with reference


#1
#[derive(Debug)]
struct Foo {
    f: String,
}
fn main() {
    let a = Foo { f: String::from("hello") };
    let y: &Foo;
    if false {
        // borrow
        let x = &a;
        y = &a;
    }
   println!("here {}", y.f);
}

Hi , I’m new in rust , and have one question , why this code return this error "use of possibly uninitialized variable: y.f"
if Y is in scope and A is a valid reference?
Thansk.


#2

Rust doesn’t evaluate control flow like this and sees that the ref is assigned only conditionally.