News for hackers. Stuff that matters.
Re:A half-hour to learn Rust by kzrdude on Saturday February 29, 2020 @05:28AM (#22449960)
Underscore is not exactly "throw away" but rather it is exactly "don't bind to variable". The difference becomes evident with the statement:

    let _ = x;

This is a no-op and the value remains owned by the variable x, and is not thrown away.
Re:A half-hour to learn Rust by fasterthanlime on Saturday February 29, 2020 @07:04AM (#22450171)
I debated which terminology to use and thought "throwing away" was more intuitive, especially if you've never heard of "binding" before.

It has its limits though - in your example I'd say you're throwing away the result of evaluating "x", just as if you did:

    x;
Re:A half-hour to learn Rust by kzrdude on Saturday February 29, 2020 @07:50AM (#22450295)
I don't agree, because we can try the two following programs, and one of them does not compile. It compiles with underscore.

1:

    let x = String::new();
    let _ = x;
    println!("{}", x);
2:

    let x = String::new();
    x;
    println!("{}", x);   // ERROR: Use of moved value x.

And this is why I said that let _ = x; is a no-op. :)
Re:A half-hour to learn Rust by fasterthanlime on Saturday February 29, 2020 @12:00PM (#22451517)
Huh, TIL! Had no idea `x;` actually moved `x`.
Re:A half-hour to learn Rust by AnimalMuppet on Saturday February 29, 2020 @12:38PM (#22451772)
But where is it moved to? Who owns it now? Or is it just lost?
Re:A half-hour to learn Rust by kzrdude on Saturday February 29, 2020 @01:15PM (#22451993)
yes, it is dropped
Re:A half-hour to learn Rust by runarberg on Saturday February 29, 2020 @03:00PM (#22452663)
Ahh, I remember a while back reading a blog where the author talked about `std::mem::drop` being their favorite standard library function because they used this effect. It is literately defined as:

    pub fn drop<T>(_x: T) { }
https://doc.rust-lang.org/std/mem/fn.drop.html
Re:A half-hour to learn Rust by kzrdude on Saturday February 29, 2020 @05:10PM (#22453516)
Right, so drop is useless :) We can drop things fine without it. Since of course it's scope/ownership based and not something that's explicitly called.
Re:A half-hour to learn Rust by AnimalMuppet on Saturday February 29, 2020 @07:12PM (#22454181)
Well,

  drop(x);
is a lot clearer than

  x;
Re:A half-hour to learn Rust by e_proxus on Sunday March 01, 2020 @06:13AM (#22456363)
So is drop like free, or is it something else?
Re:A half-hour to learn Rust by cyphar on Sunday March 01, 2020 @08:07AM (#22456706)
Yes, it's effectively like free -- though C++'s "auto" is probably a closer idea.

The main difference to C's free is that you simply cannot double-free a value (without using unsafe). That's because a value is only dropped when it falls out of scope -- and since it values only have one owner (and references must not outlive values) there cannot be any dangling references after it's dropped.

The documentation for std::mem::drop is explaining that dropping a value is a property of the language and scoping rules -- thus there is nothing for the function to do other than take a value and let it go out of scope.

Re:A half-hour to learn Rust by pixel_fcker on Sunday March 01, 2020 @12:13PM (#22457812)
It’s more like delete in c++ because you can implement the Drop trait for a type in order to run code when it is dropped in order to clean up resources etc.
Re:A half-hour to learn Rust by kzrdude on Sunday March 01, 2020 @04:29PM (#22459499)
Drop is just what runs when a value goes out of scope. Like destructors in C++.
Re:A half-hour to learn Rust by kzrdude on Saturday February 29, 2020 @01:56PM (#22452233)
When you put it like that, I see that it's a rather minor detail.
Re:A half-hour to learn Rust by Tehnix on Saturday February 29, 2020 @01:44PM (#22452151)
I think no-op is a bit misleading, and throwing away is more precise, since Rust has strict evaluation.

    let _ = timeConsumingFunction();
Still needs to spend time performing timeConsumingFunction(). It might not move, but it still did the work and produced a result, you are just not using it (and hence not need to move it).

If you you had lazy evaluation I would agree with your argument for the no-op terminology.

Re:A half-hour to learn Rust by kzrdude on Saturday February 29, 2020 @01:57PM (#22452241)
That statement is different, the rhs is not a variable's name, so I agree with you, all of that is not a no-op.
Re:A half-hour to learn Rust by aallen90 on Sunday March 01, 2020 @11:22AM (#22457500)
In C#, _ is called a "discard" variable. I found that to be an apt term to grasp the concept quickly.
Re:A half-hour to learn Rust by nightcracker on Saturday February 29, 2020 @06:15AM (#22450063)
Underscore can also be the equivalent of a wildcard in pattern matching.
Re:A half-hour to learn Rust by saghm on Saturday February 29, 2020 @01:30PM (#22452077)
I think that's the same thing as in let bindings; just like you can bind to a name in the wild card case, you can also choose not to bind to it. The main difference is that not binding in the wild card case is more common than in `let` bindings.
Re:A half-hour to learn Rust by da-x on Sunday March 01, 2020 @03:28AM (#22455805)
There's a difference between

    let _ = <expr>;
and:

    let _x = <expr>;
The first one drops ownership over the variable right after `let`, and the second drops in the end of the scope where the `let` is contained. It was mentioned in an open Rust github issue, but I can't seem to find it.
Re:A half-hour to learn Rust by watmough on Saturday February 29, 2020 @10:51AM (#22451129)
I did wonder about this, due to consumption of ... values / refs (terminology?) ... in Rust.

Thanks for adding this important detail.

Re:A half-hour to learn Rust by monocasa on Saturday February 29, 2020 @01:04PM (#22451935)
I guess I always viewed that as dropping a reference which is a noop, but still dropping.