comprehensive-rust icon indicating copy to clipboard operation
comprehensive-rust copied to clipboard

Details in variables.md is a bit confusing

Open jamiecha opened this issue 2 years ago • 2 comments

In the second item of Details in variables.md, it says :

Note that since println! is a macro, x is not moved, even using the function like syntax of println!("x: {}", x)

But it's not 100% true because variable x in the example code is i32 type which will never get moved by calling both pure function or macro. I think either sample code or description should be changed somehow.

Please check the code below

  • x never get moved so my_print() can be called multiple times without error.
fn my_print(x: i32) {
    println!("Hello {}", x);
}

fn main() {
    let x: i32 = 10;
    my_print(x);
    my_print(x);
}

Rust Playground

jamiecha avatar Mar 22 '23 04:03 jamiecha

The statement is true -- the x in the chapter is neither moved nor copied. But, you're correct that it is not a great way to illustrate the point. Perhaps this would be better:

fn main() {
    let s = String::from("abc");
    println!("{}", s);
    println!("{}", s);
}

djmitche avatar May 05 '23 16:05 djmitche

Honestly, at this point in the course, we haven't talked about moving at all, so the speaker notes could probably be simplified.

mgeisler avatar May 08 '23 08:05 mgeisler