在 CI 中可以使用尚未初始化的结构。我在 Rust 中尝试了这段代码:
struct User {
username: String,
email: String,
sign_in_count: u64,
active: bool,
}
fn main(){
let mut user1: User;
user1.active = false;
}
但它产生了编译错误:
error[E0381]: assign to part of possibly-uninitialized variable: `user1`
--> src/main.rs:10:5
|
10 | user1.active = false;
| ^^^^^^^^^^^^^^^^^^^^ use of possibly-uninitialized `user1`
为什么这在 C 中是允许的,但在 Rust 中是错误的?