[Compiler] why does compiler use var type as reference

In this fn, x is String type, then move x to y. In the compiler x is String type, but y is &String type.

fn main() {
let x: String;
x = String::from("a");

let y = x

}

In compiler MIR:
debug x => _1;
let _1: std::string::String
_2 = const<String as From<&str>>::from("a")
_1 = move _2;

debug y ==> _3;
let _3:&std::string::String;
_3 = move _1;

  1. Fomat your code.

  2. That's not what I see. I got this:

fn main() -> () {
    let mut _0: ();                      // return place in scope 0 at src/main.rs:1:11: 1:11
    let _1: std::string::String;         // in scope 0 at src/main.rs:2:9: 2:10
    let mut _2: std::string::String;     // in scope 0 at src/main.rs:3:9: 3:26
    scope 1 {
        debug x => _1;                   // in scope 1 at src/main.rs:2:9: 2:10
        let _3: std::string::String;     // in scope 1 at src/main.rs:5:9: 5:10
        scope 2 {
            debug y => _3;               // in scope 2 at src/main.rs:5:9: 5:10
        }
    }

    bb0: {
        _2 = <String as From<&str>>::from(const "a") -> bb2; // scope 1 at src/main.rs:3:9: 3:26
                                         // mir::Constant
                                         // + span: src/main.rs:3:9: 3:21
                                         // + user_ty: UserType(2)
                                         // + literal: Const { ty: fn(&str) -> String {<String as From<&str>>::from}, val: Value(<ZST>) }
                                         // mir::Constant
                                         // + span: src/main.rs:3:22: 3:25
                                         // + literal: Const { ty: &str, val: Value(Slice(..)) }
    }

    bb1: {
        return;                          // scope 0 at src/main.rs:6:2: 6:2
    }

    bb2: {
        _1 = move _2;                    // scope 1 at src/main.rs:3:5: 3:6
        _3 = move _1;                    // scope 1 at src/main.rs:5:13: 5:14
        drop(_3) -> bb1;                 // scope 1 at src/main.rs:6:1: 6:2
    }
}

I got this fn from a rust-lab presentation:

Im a beginner of rustc. I dont know what happend neither. :sweat_smile:

Sorry. Can't watch the video at work. You can check it yourself at the playground. There's a Show MIR option.

1 Like

of course, thanks a lot

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.