Вход на сайт

Просмотр новости

Найдите то, что Вас интересует

Array initialization post declaration

Дата публикации: 06-10-2026 02:07:32


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

Схожие новости

#Наименование новостиТональностьИнформативностьДата публикации
1Arena's and alignment07.304-10-2026
2Why no Dependent Matrix Type?05.1608-10-2026
3Type erasure and dyn lifetime in a NonNull/Box struct field08.5504-10-2026
4Why is f32/f64::sqrt not const?022.7206-10-2026
5Optimization of inlined functions returning Result?07.3606-10-2026
6What's everyone working on this week (41/2026)?020.3406-10-2026
7Rust abstractions for kernel sources04.6208-10-2026
8Trait object with associated types in field - requires generic param?011.0807-10-2026
9Declarative Macros in Rust: A Simple and Practical Introduction012.6406-10-2026
10Strategies for dyn trait objects with optional behavior?07.1206-10-2026

Классификация: . Схожих патентов: 0. Схожих новостей: 10. Тональность: 0. Информативность: 16.79. Источник: users.rust-lang.org.