Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Syntax to create dynamic-length homogenous vectors #4221

Closed
geoffhill-zz opened this issue Dec 19, 2012 · 1 comment
Closed

Syntax to create dynamic-length homogenous vectors #4221

geoffhill-zz opened this issue Dec 19, 2012 · 1 comment

Comments

@geoffhill-zz
Copy link
Contributor

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];
}
@geoffhill-zz
Copy link
Contributor Author

Noticed that this has already been resolved in 0.5.

RalfJung pushed a commit to RalfJung/rust that referenced this issue Mar 28, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant