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__(inout self):
pass
fn __moveinit__(inout self, owned existing: Self):
print("moving")
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^)
fn return_foo[T: Movable](owned foo: T) -> T:
return foo^
var foo = Foo()
var res = return_foo(foo^)
moving
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.
Was this page helpful?
Thank you! We'll create more content like this.
Thank you for helping us improve!
😔 What went wrong?