Does this mean the variable I created is performing a move?

There are no errors here, and I'm a bit curious. When I call a function terima_nilai into a variable pengguna and then provide a String as a parameter to be passed to the function terima_nilai, am I also performing a move on the variable to terima_nilai?

I don't quite understand because the parameter in the function called on that variable is not like the original value of the variable, its only parameter for terima_nilai.

struct User {
    nama_tanaman: String,
    nama_latin: String,
    tahun_penemuan: i32,
}
fn terima_nilai (nama_tanaman: String, nama_latin: String) -> User {
    User {
        nama_tanaman,
        nama_latin,
        tahun_penemuan: 1000,
    }
}

fn main () {
// Is this transfer my ownership for pengguna to fn terima_nilai?
// Then fn terima_nilai transfer back the ownership to pengguna?
    let pengguna = terima_nilai(
        String::from("Wortel"),
        String::from("Daucus carota"),
    );
}

The strings move into terima_nilai, which moves them into a new User. It moves User back to the caller, which moves it to pengguna

1 Like

Does the var pengguna also move terima_nilai or only the param?

Only the params. The var doesn't exist until terima_nilai returns. This is the conceptual model.

1 Like

Got it, thank you.
Regards, qoori.

The ABI model is a different story, but the rules of Rust are defined on the conceptual model.

4 Likes