Skip to content

More flexible initialization for vectors #12680

Closed
@kvanbere

Description

@kvanbere

In one of my projects which I'd like to port to rust in the long term, there is some C99 compliant array initializer like so;

  static const void *dispatch[255] = {
    [opcode_addk] = &&opc_addk,
    [opcode_addr] = &&opc_addr,
    [opcode_addvk] = &&opc_addvk,
    [opcode_addvr] = &&opc_addvr,
    [opcode_inc] = &&opc_inc,
    [opcode_subk] = &&opc_subk,
    [opcode_subr] = &&opc_subr,
    [opcode_subvk] = &&opc_subvk,
    [opcode_subvr] = &&opc_subvr,
    [opcode_dec] = &&opc_dec,
    &&opc_unident,
  };

Here opc_unident fills the parts of the array not specified, and each [index] = val pair puts val at the respective index. This cuts down the boilerplate for some tasks a LOT (In rust, I would have to write out all 255 entries for it to be quickly "maintainable").

I propose some syntax is added for complex initialization of vectors in a similar way.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions