Appearance
变量3:exercises/variables/variables3.rs
题目
rust
// variables3.rs
//
// Execute `rustlings hint variables3` or use the `hint` watch subcommand for a
// hint.
// I AM NOT DONE
fn main() {
let x: i32;
println!("Number {}", x);
}
题目解析
编译错误信息显示如下:
txt
⚠️ Compiling of exercises/variables/variables3.rs failed! Please try again. Here's the output:
error[E0381]: used binding `x` isn't initialized
--> exercises/variables/variables3.rs:10:27
|
9 | let x: i32;
| - binding declared here but left uninitialized
10 | println!("Number {}", x);
| ^ `x` used here but it isn't initialized
|
= note: this error originates in the macro `$crate::format_args_nl` which comes from the expansion of the macro `println` (in Nightly builds, run with -Z macro-backtrace for more info)
help: consider assigning a value
|
9 | let x: i32 = 0;
| +++
error: aborting due to previous error
For more information about this error, try `rustc --explain E0381`.
在这个练习中,我们先是创建了一个变量绑定let x: i32;
,然后尝试使用它,但是错误信息提示我们定义了绑定,但是我们没有对x
进行初始化,自然是无法打印一个不存在的东西。
所以如错误提示那样,我们需要给变量x
赋值,比如let x: i32 = 0;
rust
fn main() {
let x: i32;
let x: i32 = 0;
println!("Number {}", x);
}
笔记
- 变量需要先进行初始化,才能使用