You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
It would be nice if _ were allowed instead of an expression for the length in array types and array expressions, in cases where the length can be inferred based on context:
Reacted by est31, Hanna Kruppe, Eduard-Mihai Burtescu, Martin Carton, Jeff Burdges, oli, Jake Goulding, Dan Kolsoi, Ted Mielczarek, Vitaly Shukela and 22 more
This would have to be a part of rust-lang/rfcs#1931, you should leave a comment there perhaps.
The reason is that whenever you have a generic parameter (lifetime / type / const), you need inference variables to instantiate, at least in the (slightly modified) HM typesystem we use.
A simple example is that Vec::new is <Vec<_>>::new, and _ creates an unique type inference variable.
Similarly, if Vec had a from_array method with a const parameter, Vec::from_array would require a const inference variable, at least internally in the compiler, and so would any trait impl.
Now, we could just not provide a surface syntax for this, but I think reusing _ is probably fine.
When I wrote the above it didn’t feel like an entirely new language feature, just a "simple" extension of the existing inference mechanism. But I can imagine how from an compiler internals point of view it could require most of the plumbing for const parameters in types.
This is definitely something that should be part of the language spec - definitely not somewhere macros shine.
A particular pain point with rust arrays comes when adding a member to a previously declared array or when declaring an array without knowing in that moment how long it will be.
For example:
constARRAY:[u8;2] = [1,2];
Coming back to it later and adding 3 to the array requires changing two places. This is quite obvious in this example, but when dealing with arrays with many (complex) elements spanning multiple lines or even pages, remembering to go up and change the signature is challenging.
Additionally, when sitting down to write code for the first time, it is often the case that you don't know until you finish writing it how many members your array will have. For example,
constVALID_EXTENSIONS:[String, _] = ["ext1","ext2","ext3",// let's stop and think for a while. What other extensions could we include here?// oh yes!"ext4","ext5",}
Only having reached the end of the list does one know how many they'll have. Painful.
Reacted by Nayuki, Félix, Jan Katzer, Robert Grosse, Pedro Nobre, raffimolero, Artur J., Streq and Calin Baenen
@mqudsi In the general case, const generics will have to include _ for values, at least internally in the implementation (so we could easily support the _ syntax as well).
However, in your example, it's also like rust-lang/rfcs#2010, in that the type is inferred from the RHS.
cc @Centril Maybe we can revisit that RFC? I think some of the concerns were overblown.
The use-case for this seems incredibly clear. Am I understanding you all correctly when I interpret the counter-point as being that the meaning of the _-symbol would become less clear and coherent across the language? If so then could the problem just be solved by using an alternative syntax?
This is definitely something that should be part of the language spec - definitely not somewhere macros shine.
A particular pain point with rust arrays comes when adding a member to a previously declared array or when declaring an array without knowing in that moment how long it will be.
For example:
constARRAY:[u8;2] = [1,2];
Coming back to it later and adding 3 to the array requires changing two places. This is quite obvious in this example, but when dealing with arrays with many (complex) elements spanning multiple lines or even pages, remembering to go up and change the signature is challenging.
Additionally, when sitting down to write code for the first time, it is often the case that you don't know until you finish writing it how many members your array will have. For example,
constVALID_EXTENSIONS:[String, _] = ["ext1","ext2","ext3",// let's stop and think for a while. What other extensions could we include here?// oh yes!"ext4","ext5",}
Only having reached the end of the list does one know how many they'll have. Painful.
For me, it's not even about obviousness. I have started to pick up development on my game again, and I was modifying an associated constant, PlayerSkin::MENU_INFO, which had type [X; 13], but since the number ov skins changed, I also had to change the number in the array definition.
I know it doesn't seem like a big deal in my case; however, it's just slightly obnoxious that I have to go out ov my way to change something other than the array contents for something that could, reasonably, be inferred at compile-time, given that my array is constant.
EDIT:
¹ most recent discussion I can find on inferring the type of const/static is rust-lang/rfcs#3546 (comment) (sounded promising, but that was a year ago)
FWIW, I remain more interested in other approaches than in just making it allowed. See things like rust-lang/rfcs#2545 (comment) for a pie in the sky wish. (Speaking as me, not as lang as a team.)
Or, alternatively, I wonder if -- for places where people don't want to commit to a specific length -- we should just allow
or similar. After all, if the author doesn't want to commit to a length, let [a, b, c] = VALID_EXTENSIONS; being a fallible pattern sounds like a good thing.
EDIT: ¹ most recent discussion I can find on inferring the type of const/static is rust-lang/rfcs#3546 (comment) (sounded promising, but that was a year ago)
Sorry for the late response, @eddyb. I have been unable to reply here because another GitHub user – whether or not through legitimate, authorized means – restricted my access to the rust-lang organization for reasons which can not be disclosed under the Rust language organizations Code of Conduct.
I don't see why arrays can't be an exception to the const-type inference rule, since I would imagine the length is something the compiler could easily figure out. (At least for array literals. Though, outside ov array literals, I do not know how else one would create a const array value.)
After looking at the linked RFC, it seems tangentially related; though, it's more focused on types themselves, rather than const generics (i.e. length ov arrays) – and, in fact, one example shown in that RFC uses [_; 4].
Also, as a previous comment stated, I feel like using macros to solve this is not an elegant solution; this feels like something the language should have an innate method ov solving.
FWIW, I remain more interested in other approaches than in just making it allowed. See things like rust-lang/rfcs#2545 (comment) for a pie in the sky wish. (Speaking as me, not as lang as a team.)
Or, alternatively, I wonder if -- for places where people don't want to commit to a specific length -- we should just allow
or similar. After all, if the author doesn't want to commit to a length, let [a, b, c] = VALID_EXTENSIONS; being a fallible pattern sounds like a good thing.
I feel like after the type there should be a _, or there should at least be something after the type – even after seeing your other comment – because it could confuse both the person reading the type, as well as the Rust compiler, into thinking that someone is trying to declare a slice ov Strings.
(Though, I'm sure you already thought ov that, as, in your other comment, you suggested a slightly different syntax: [T; numeric_type].)
Activity
eddyb commentedon Mar 5, 2017
This would have to be a part of rust-lang/rfcs#1931, you should leave a comment there perhaps.
The reason is that whenever you have a generic parameter (lifetime / type / const), you need inference variables to instantiate, at least in the (slightly modified) HM typesystem we use.
A simple example is that
Vec::newis<Vec<_>>::new, and_creates an unique type inference variable.Similarly, if
Vechad afrom_arraymethod with a const parameter,Vec::from_arraywould require a const inference variable, at least internally in the compiler, and so would any traitimpl.Now, we could just not provide a surface syntax for this, but I think reusing
_is probably fine.SimonSapin commentedon Mar 5, 2017
When I wrote the above it didn’t feel like an entirely new language feature, just a "simple" extension of the existing inference mechanism. But I can imagine how from an compiler internals point of view it could require most of the plumbing for const parameters in types.
durka commentedon Mar 5, 2017
See counted-array :)
mqudsi commentedon Aug 12, 2018
This is definitely something that should be part of the language spec - definitely not somewhere macros shine.
A particular pain point with rust arrays comes when adding a member to a previously declared array or when declaring an array without knowing in that moment how long it will be.
For example:
Coming back to it later and adding
3to the array requires changing two places. This is quite obvious in this example, but when dealing with arrays with many (complex) elements spanning multiple lines or even pages, remembering to go up and change the signature is challenging.Additionally, when sitting down to write code for the first time, it is often the case that you don't know until you finish writing it how many members your array will have. For example,
Only having reached the end of the list does one know how many they'll have. Painful.
eddyb commentedon Aug 18, 2018
@mqudsi In the general case, const generics will have to include
_for values, at least internally in the implementation (so we could easily support the_syntax as well).However, in your example, it's also like rust-lang/rfcs#2010, in that the type is inferred from the RHS.
cc @Centril Maybe we can revisit that RFC? I think some of the concerns were overblown.
fiso commentedon Mar 6, 2019
The use-case for this seems incredibly clear. Am I understanding you all correctly when I interpret the counter-point as being that the meaning of the
_-symbol would become less clear and coherent across the language? If so then could the problem just be solved by using an alternative syntax?How about simply:
Or alternatively, introduce a new symbol that carries no problematic baggage and use that in place of
_for this kind of situation.Or am I just misunderstanding the objections?
steveklabnik commentedon Mar 7, 2019
Given that this pretty clearly requires an RFC, I'm going to give it a close.
Centril commentedon Mar 7, 2019
cc rust-lang/rfcs#2545
CalinZBaenen commentedon May 26, 2026
For me, it's not even about obviousness. I have started to pick up development on my game again, and I was modifying an associated constant,
PlayerSkin::MENU_INFO, which had type[X; 13], but since the number ov skins changed, I also had to change the number in the array definition.I know it doesn't seem like a big deal in my case; however, it's just slightly obnoxious that I have to go out ov my way to change something other than the array contents for something that could, reasonably, be inferred at compile-time, given that my array is
constant.It would be nice if I could just replace the 11 with an underscore (
_) or something and never have to worry about it again.eddyb commentedon May 26, 2026
For the record, since this issue was closed, this now works:
The problem with the
constcase is that no part of the type of aconstmay be inferred¹ from the value.A macro can work around this by duplicating your array expression:
EDIT:
¹ most recent discussion I can find on inferring the type of
const/staticis rust-lang/rfcs#3546 (comment) (sounded promising, but that was a year ago)Scripter17 commentedon May 26, 2026
scottmcm commentedon Jun 2, 2026
FWIW, I remain more interested in other approaches than in just making it allowed. See things like rust-lang/rfcs#2545 (comment) for a pie in the sky wish. (Speaking as me, not as lang as a team.)
Or, alternatively, I wonder if -- for places where people don't want to commit to a specific length -- we should just allow
or similar. After all, if the author doesn't want to commit to a length,
let [a, b, c] = VALID_EXTENSIONS;being a fallible pattern sounds like a good thing.CalinZBaenen commentedon Jun 18, 2026
Sorry for the late response, @eddyb. I have been unable to reply here because another GitHub user – whether or not through legitimate, authorized means – restricted my access to the rust-lang organization for reasons which can not be disclosed under the Rust language organizations Code of Conduct.
I don't see why arrays can't be an exception to the
const-type inference rule, since I would imagine the length is something the compiler could easily figure out. (At least for array literals. Though, outside ov array literals, I do not know how else one would create aconstarray value.)After looking at the linked RFC, it seems tangentially related; though, it's more focused on types themselves, rather than
constgenerics (i.e. length ov arrays) – and, in fact, one example shown in that RFC uses[_; 4].Also, as a previous comment stated, I feel like using macros to solve this is not an elegant solution; this feels like something the language should have an innate method ov solving.
CalinZBaenen commentedon Jun 18, 2026
I feel like after the type there should be a
_, or there should at least be something after the type – even after seeing your other comment – because it could confuse both the person reading the type, as well as the Rust compiler, into thinking that someone is trying to declare a slice ovStrings.(Though, I'm sure you already thought ov that, as, in your other comment, you suggested a slightly different syntax:
[T; numeric_type].)theemathas commentedon Jun 19, 2026