Reputation: 1876
I have a 3-element array:
let color = [0.25, 0.25, 0.25];
I'd like to turn it into a 4-element array, which would be the 3-element array plus one appended element:
let color_with_alpha = [color[0], color[1], color[2], 1.0];
I know Rust has some cool syntax sugar for a lot of things; is there something for this? Something like:
let color_with_alpha = [color, 1.0];
I've read about the concat
macro but that seems to only create string slices. I would imagine there's a vector-based solution but I don't require the dynamic sizing.
Upvotes: 2
Views: 343
Reputation: 432179
No, there is no such syntax.
It's always hard to prove a negative, but I've implemented a parser of Rust code and I've used Rust for over 3 years; I've never encountered any such syntax.
The closest I can imagine would be to implement a trait for array of various sizes. This is complicated because you cannot move out of non-Copy
arrays. Since there's no generic integers, you'd have to implement this trait for every size of array you needed.
trait ArrayAppend<T> {
type Output;
fn append(self, val: T) -> Self::Output;
}
impl<T: Copy> ArrayAppend<T> for [T; 3] {
type Output = [T; 4];
fn append(self, val: T) -> Self::Output {
[self[0], self[1], self[2], val]
}
}
fn main() {
let color = [0.25, 0.25, 0.25];
let color2 = color.append(1.0);
}
Upvotes: 3