I've recently opened https://github.com/graydon/rust/issues/917 for
this, but the mailing list is probably a better channel for
discussion.

Rust currently spells moving data as 'x <- y' (move y into x). You can
initialize locals by move, and move into arbitrary lvals with this
binary op.

Beyond this, there is move mode for function arguments (your value
will automatically be moved -- its ownership passed to the function)
when you call such a function. And the compiler will automatically
replace copies with moves in some cases where it can prove that the
source value will no longer be used.

But it is not currently possible to move something in when
initializing a data structure or calling a function that doesn't take
its argument by move. The specific case in which I had this problem
was...

    let x = [];
    for y in big_collection { x += [something(y)]; }
    ret @x;

The return will *copy* the array, whereas I don't need two copies, I
just want to box the one I already have.

I see two good solutions to this problem, and I'm wondering which of
these other people prefer:

- The simple, explicit approach: Introduce an opeator, called 'move',
or unary '<' if we want to be clever (we'd get 'x =< y' and could get
rid of '<-'), which tells the compiler the value can be moved. You
could do 'ret @<x;', 'f(<x)' etcetera.

- The implicit, clever approach: Notice that the only situation where
you want to do this is when using a local variable (you can't move out
of data structures) for the last time, and simply make the compiler
optimize the last use of a variable into a move. We'll probably want
to do this anyway, but the question is: Is it enough, or do people
want to 'see' their move operations in front of them?

The observation that the only sane place to use '<-' is the last use
of a variable also casts doubt on the utility of '<-' as a variable
initializer -- you'd basically just be renaming a variable.

Note that all this assumes that temporary values are always 'moved'.
This is currently an optimization in the compiler. Once we implement
destination-passing style, temporaries will be initialized into the
right location, so there won't even be a move happening anymore.
_______________________________________________
Rust-dev mailing list
Rust-dev@mozilla.org
https://mail.mozilla.org/listinfo/rust-dev

Reply via email to