worik
December 4, 2018, 10:08pm
1
If I have the code:
const fixed_thing[usize;3] = [1,2,3,4];
I get error:
29 | const fixed_thing:[usize;3] = [1,2,3,4];
| ^^^^^^^^^ expected an array with a fixed size of 3 elements, found one with 4 elements
|
= note: expected type `[usize; 3]`
found type `[usize; 4]`
Fair enough. Except why can I not write:
const fixed_thing:[usize;] = [1,2,3,4];
The compiler has all the information that it needs to determine the size of the object. What am I missing?
This has been asked before
Been running into the issue listed at static - How to specify const array in global scope in Rust? - Stack Overflow .
Specifically:
const NUMBERS: [i32] = [1, 2, 3, 4, 5];
error[E0308]: mismatched types
--> src/main.rs:2:28
|
2 | const NUMBERS: [i32] = [1, 2, 3, 4, 5];
| ^^^^^^^^^^^^^^^ expected slice, found array of 5 elements
|
= note: expected type `[i32]`
= note: found type `[i32; 5]`
error[E0277]: the trait bound `[i32]: std::marker::Sized` is…
opened 12:21AM - 05 Mar 17 UTC
closed 04:07PM - 07 Mar 19 UTC
A-const-fn
A-inference
C-feature-request
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:
```rust
fn main() {
let a: [u8; 4] = [0; _];
let b: [u8; _] = a;
}
```
rustc 1.17.0-nightly (b1e31766d 2017-03-03)
```rust
error: expected expression, found `_`
--> a.rs:2:26
|
2 | let a: [u8; 4] = [0; _];
| ^
error: expected expression, found `_`
--> a.rs:3:17
|
3 | let b: [u8; _] = a;
| ^
error: aborting due to 2 previous error
```
In comparison, the item type in array expressions and array types can be inferred today:
```rust
fn main() {
let a: [u8; 4] = ["0".parse::<_>().unwrap(); 4];
let b: [_; 4] = a;
}
```
and const generics may help with it
opened 11:17PM - 14 Sep 17 UTC
closed 03:26PM - 21 Mar 22 UTC
A-typesystem
B-RFC-approved
T-lang
T-compiler
A-const-fn
C-tracking-issue
A-const-generics
requires-nightly
F-const_generics
S-tracking-impl-incomplete
Tracking issue for rust-lang/rfcs#2000
Updates:
- 2 May 2019: https://github… .com/rust-lang/rust/issues/44580#issuecomment-488819344
- 19 Oct 2019: https://github.com/rust-lang/rust/issues/44580#issuecomment-544155666
- 2 Jan 2020: https://github.com/rust-lang/rust/issues/44580#issuecomment-570191702
- 22 Jul 2020: https://github.com/rust-lang/rust/issues/44580#issuecomment-662543117
- 17 Nov 2020: https://github.com/rust-lang/rust/issues/44580#issuecomment-728913127
- 11 Dez 2021: https://github.com/rust-lang/rust/issues/44580#issuecomment-991782799
If you want to help out, take a look at the [open const generics issues](https://github.com/rust-lang/rust/labels/A-const-generics) and feel free to ping @varkor, @eddyb, @yodaldevoid, @oli-obk or @lcnr for help in getting started!
---
Blocking stabilization:
- [ ] Design:
- [x] Resolving ordering of const and type parameters, with default parameters
- [ ] Decide what the best UX / implementation cost balance is for unifying abstract const expressions.
- [ ] How we determine well formedness of const expressions.
- [x] Implementation
- [ ] Documentation
- [ ] rustc guide
---
Remaining implementation issues:
- [ ] Resolve various `FIXME(const_generics)` comments.
- [ ] Resolve concerns with canonicalisation / lazy normalisation.
- [ ] Investigate handling of const parameters in patterns.
- [ ] Add more tests.
- [ ] Implement defaults for const parameters (`FIXME(const_generics_defaults)`).
- [ ] Fix other [A-const-generics issues](https://github.com/rust-lang/rust/labels/A-const-generics).
- [ ] Audit uses of `has_infer_types`.
- [x] Forbid complex expressions for const arguments involving parameters (for now), e.g. `{X * 2}`.
- [ ] Audit diagnostics (e.g. https://github.com/rust-lang/rust/pull/76401#discussion_r484819320).
1 Like