#[allow(unused_variables)] fn main() { let unused_variable = 42; // 未使用的变量 } 如果只想禁用特定变量的未使用警告,可以将注解添加到该变量的行上。 示例代码如下: 代码语言:txt 复制 fn main() { let used_variable = 42; #[allow(unused_variables)] let unused_variable = 42; // 未使用...
warning: unused variable: `f` --> src\main.rs:4:9 | 4 | let f = File::open("hello.txt").expect("打开文件hello.txt出错!"); | ^ help: if this is intentional, prefix it with an underscore: `_f` | = note: `#[warn(unused_variables)]` on by default warning: 1 warning emitt...
输出结果:Compiling playground v0.0.1 (/playground)warning: unused variable: `x` --> src/main.rs:2:9 |2 | let x = 4; | ^ help: if this is intentional, prefix it with an underscore: `_x` | = note: `#[warn(unused_variables)]` on by defaultwarning: `playground` ...
warning: unused variable: `x` --> src/main.rs:2:9 | 2 | let x = 4; | ^ help: if this is intentional, prefix it with an underscore: `_x` | = note: `#[warn(unused_variables)]` on by default warning: `playground` (bin "playground") generated 1 warning Finished dev [unoptimi...
warning: variable `a` is assigned to, but never used --> var.rs:2:9 | 2 | let a; | ^ | = note: `#[warn(unused_variables)]` on by default = note: consider using `_a` instead 这是因为我们的变量在声明或者赋值以后,完全没有使用它的值。Rust希望通过这样的警告来避免无用代码,以保...
0 (/Users/Work/Code/Rust/student/variables) warning: unused variable: `guess` --> src/main.rs:2:9 | 2 | let guess: u32 = "42".parse().expect("這不是數字!"); | ^^^ help: consider prefixing with an underscore: `_guess` | = note: `#[warn(unused_variables)]` on by defaul...
Compiling hello_world v0.0.1 (file:///home/you/projects/hello_world) src/main.rs:2:9: 2:10 warning: unused variable: `x`, #[warn(unused_variable)] on by default src/main.rs:2 let x: i32; ^ Rust 警告我们从来没有使用这个变量绑定,但是因为我们从来没有使用它,没有破坏性,没有恶意。
= note: `#[warn(unused_variables)]` on by defaultwarning: unused variable: `d` --> src/main.rs:21:9 | 21 |letd = CustomSmartPointer {data: String::from("other stuff")}; | ^help:ifthis is intentional, prefix it with an underscore: `_d` ...
warning:unused variable:`y`-->ownership.rs:3:9|3|lety=x;|^help:ifthisis intentional,prefix itwithan underscore:`_y`|=note:`#[warn(unused_variables)]`on bydefaulterror[E0382]:borrowofmoved value:`x`-->ownership.rs:4:23|2|letx=String::from("hello");|-move occurs because`x`has ...
=note: `#[warn(unused_must_use)]`onbydefault =note: this `Result` may bean`Err` variant,whichshould be handled 1.1 匹配不同的错误原因 在处理错误时,很多时候需要针对不同的错误原因进行不同的处理。下面来学习一下rust标准库中的std::iomodule中是如何设计错误处理的。