Hello, this is my first forum post. I've been reading through the rust book and trying to work on some basic implementations of linear algebra stuff in rust and came across this code that doesn't compile
fn main() {
let a: [usize; 1];
a[0] = 2;
println!("{}", a[0]);
}
this is what the compiler outputs when given the code:
error[E0381]: used binding `a` isn't initialized
--> temp3.rs:4:5
|
2 | let a: [usize; 1];
| - binding declared here but left uninitialized
3 |
4 | a[0] = 2;
| ^^^^ `a` used here but it isn't initialized
|
help: consider assigning a value
|
2 | let a: [usize; 1] = [42; 1];
| +++++++++
error: aborting due to 1 previous error
For more information about this error, try `rustc --explain E0381`.
i understand that this can be fixed by just initializing the array with any number but i was wondering why exactly the compiler refuses to compile this, does it somehow result in possible unsafe patterns?
6 posts - 4 participants
Read full topic
| # | Наименование новости | Тональность | Информативность | Дата публикации |
|---|---|---|---|---|
| 1 | Arena's and alignment | 0 | 7.3 | 04-10-2026 |
| 2 | Why no Dependent Matrix Type? | 0 | 5.16 | 08-10-2026 |
| 3 | Type erasure and dyn lifetime in a NonNull/Box struct field | 0 | 8.55 | 04-10-2026 |
| 4 | Why is f32/f64::sqrt not const? | 0 | 22.72 | 06-10-2026 |
| 5 | Optimization of inlined functions returning Result? | 0 | 7.36 | 06-10-2026 |
| 6 | What's everyone working on this week (41/2026)? | 0 | 20.34 | 06-10-2026 |
| 7 | Rust abstractions for kernel sources | 0 | 4.62 | 08-10-2026 |
| 8 | Trait object with associated types in field - requires generic param? | 0 | 11.08 | 07-10-2026 |
| 9 | Declarative Macros in Rust: A Simple and Practical Introduction | 0 | 12.64 | 06-10-2026 |
| 10 | Strategies for dyn trait objects with optional behavior? | 0 | 7.12 | 06-10-2026 |