mojolang

How to add an instance of a struct to a DynamicVector


A total newbie question.

struct Item:
    var name: StringLiteral

    fn __init__(inout self):
        self.name = "Unnamed"

fn main():
    var dv = DynamicVector[Item]()
    var it = Item()
    dv.push_back(it)

Generates error: invalid call to 'push_back': method argument #0 cannot bind generic !mlirtype to memory-only type 'Item'

I don't understand the error message.

I cannot find in https://docs.modular.com/mojo/programming-manual.html the syntax for this.

Is there an equivalent to c++ vector::emplace(...)?


Solution

  • Can only store types marked @register_passable in arrays. For example Int.