Skip to main content
Log in

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")
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](owned foo: T) -> T:
return foo^

var foo = Foo()
var res = return_foo(foo^)
fn return_foo[T: Movable](owned foo: T) -> T:
return foo^

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

Implemented traits

AnyType, UnknownDestructibility

Methods

__moveinit__

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

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

Args:

  • existing (_Self): The value to move.