Skip to content

Syntax to create dynamic-length homogenous vectors #4221

Closed
@geoffhill-zz

Description

@geoffhill-zz

There should be some built-in syntax to create dynamically create vector values (both mutable and immutable) of a given size. Creating large contiguous arrays is a commonplace operation in low-level programming, and users should not have to use a library function or macro to do it. The syntax used in the following examples is not optimal for a few reasons, but illustrates the point.

For example, to create a vector of zeroes that is 20 elements long, you could use the single expressions:

@[0, ...20];
@[mut 0, ...20];

The following function would create a vector that is count elements long, and every element has value t:

fn mk_buffer<T: Copy>(count: uint, t: T) -> ~[T] {
    return ~[t, ...count]
}

This syntax could support interleaving of multiple values as well. The following function would create a vector that is count elements long, which alternates between the values t1 and t2:

fn mk_alternating_buffer<T: Copy>(count: uint, t1: T, t2: T) ⟶  ~[T] {
    return ~[t1, t2, ...count];
}

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