Skip to main content

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__(inout self):
pass

fn __moveinit__(inout 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^)
moving

Implemented traits

AnyType

Methods

__moveinit__

__moveinit__(inout self: T, owned existing: T, /)

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

Args:

  • existing (T): The value to move.