Pre-RFC: Extended array literal syntax

Calling this a pre-RFC is a bit of a stretch, but…

Wouldn’t it be cool if you could write [0, 14, 3, 72, 8; 10] and have the last element repeated 10 times? It’s a generalization of the already-supported [0, 14, 3, 72, 8] and [8; 10] literal syntaxes.

Definitely more of an “Idea” than a “Pre-RFC” :stuck_out_tongue:

I suppose it could even extend to [0;3, 1, 0;6] for [0, 0, 0, 1, 0, 0, 0, 0, 0, 0], though I don’t know if that’s a good idea.

No, I think not. Its semantics is highly non-obvious. For example, having used various kinds of scientific and numerical software and libraries (e.g. R and Numpy) I’m used to repetition applying to a whole sequence instead of just one element. But that has its own problems too (and I don’t like it either); for example, what to do when the number of specified elements doesn’t evenly divide the repeat count?

Unless this solves a real and significant problem that is impossible or prohibitively hard to solve otherwise, I’d not like to see this kind of clever but ultimately confusing shorthand syntax in Rust.


Ah that’s a good point. I hadn’t considered that interpretation. Probably best not to then :slight_smile:

This is definitely the kind of thing that’s better off as a proc-macro.

That said, I’d definitely like more flexible ways to declare large tables. I’d be nice to have e.g.

x[1..3] = [1, 2, 3];

available… though I don’t think this generalizes nicely (you need to synthesize bounds checks for slices… and size/alignment checks for trait objects, which seems super confusing).

1 Like