Skip to main content

Mojo trait

Movable

The Movable trait denotes a type whose value can be moved.

Implement the Movable trait on Foo which requires the __moveinit__ method:

struct Foo(Movable):
    fn __init__(out self):
        pass

    fn __moveinit__(out self, owned existing: Self):
        print("moving")

You can now use the ^ suffix to move the object instead of copying it inside generic functions:

fn return_foo[T: Movable](var foo: T) -> T:
    return foo^

var foo = Foo()
var res = return_foo(foo^)
moving

Implemented traits

AnyType, UnknownDestructibility

Methods

__moveinit__

__moveinit__(out self: _Self, var existing: _Self, /)

Create a new instance of the value by moving the value of another.

Args:

  • existing (_Self): The value to move.

Returns:

_Self

Was this page helpful?