[rust-dev] '-' as prefix to a function argument?

Niko Matsakis niko at alum.mit.edu
Wed Apr 11 10:24:15 PDT 2012

On 4/11/12 10:19 AM, Tim Chevalier wrote:
> The '-' prefix means that an argument is passed by move, meaning that
> when control passes to the callee, it becomes deinitialized at the
> call site. (Obviously, this means the caller has to pass an l-value.)
> This does not seem to be documented, and should be.

This is correct, except that it does not require that the caller use an 
lvalue.  The value is indeed moved from the caller to the callee---and 
thus the caller must use a value that they own.  If this value is an 
lvalue, then the lvalue must be of the sort that it can be deinitialized.

Examples might help:

fn take(-x: int) { ... }

fn give(&x: int) {
     take(x); // ERROR: x is not owned by give().

     let y = 3;
     take(y); // OK
     let z = y; // ERROR --- y was given away, can't use it anymore

     take(x+2); // OK --- rvalue

Moving vs copying is most important for unique values, as a move does 
not require that the unique value be cloned.


More information about the Rust-dev mailing list