我使用的是watch与cargo中,为了快速查看编译时错误.但是,cargo build只会在第一次构建时显示错误.
$ cargo build
Compiling clayman v0.0.1
src/core_math/vector.rs:8:5: 13:6 warning: method is never used: `New`, #[warn(dead_code)] on by default
src/core_math/vector.rs:8 pub fn New(x: i32, y: i32) -> Vector {
src/core_math/vector.rs:9 Vector {
src/core_math/vector.rs:10 x: x,
src/core_math/vector.rs:11 y: y
src/core_math/vector.rs:12 }
src/core_math/vector.rs:13 }
src/core_math/vector.rs:8:5: 13:6 warning: method `New` should have a snake case name such as `new`, #[warn(non_snake_case)] on by default
src/core_math/vector.rs:8 pub fn New(x: i32, y: i32) -> Vector {
src/core_math/vector.rs:9 Vector {
src/core_math/vector.rs:10 x: x,
src/core_math/vector.rs:11 y: y
src/core_math/vector.rs:12 }
src/core_math/vector.rs:13 }
src/main.rs:28:9: 28:10 warning: unused variable: `v`, #[warn(unused_variables)] on by default
src/main.rs:28 let v: vector::Vector;
^
$ cargo build
$
Run Code Online (Sandbox Code Playgroud)
这意味着我只能在几秒钟之前看到警告,然后watch给我一个清晰的屏幕.
有没有办法cargo build让我总是给我警告?
从 Rust 1.40 开始,Cargo 将缓存编译器消息。这意味着即使代码不需要再次编译,之前的警告也会被打印出来。
% cargo build
Compiling warnings v0.1.0 (/private/tmp/warnings)
warning: unused variable: `a`
--> src/main.rs:2:9
|
2 | let a = 42;
| ^ help: consider prefixing with an underscore: `_a`
|
= note: `#[warn(unused_variables)]` on by default
Finished dev [unoptimized + debuginfo] target(s) in 1.58s
% cargo build
warning: unused variable: `a`
--> src/main.rs:2:9
|
2 | let a = 42;
| ^ help: consider prefixing with an underscore: `_a`
|
= note: `#[warn(unused_variables)]` on by default
Finished dev [unoptimized + debuginfo] target(s) in 0.00s
Run Code Online (Sandbox Code Playgroud)
% cargo build
Compiling warnings v0.1.0 (/private/tmp/warnings)
warning: unused variable: `a`
--> src/main.rs:2:9
|
2 | let a = 42;
| ^ help: consider prefixing with an underscore: `_a`
|
= note: `#[warn(unused_variables)]` on by default
Finished dev [unoptimized + debuginfo] target(s) in 0.42s
% cargo build
Finished dev [unoptimized + debuginfo] target(s) in 0.00s
Run Code Online (Sandbox Code Playgroud)
仅当 Rust 重新编译您的文件时才会出现警告;然而,它会尽可能多地缓存,如果某些内容没有改变,它会很高兴地跳过无用的编译。目前 Cargo 中没有强制重建的选项。
一个快速但肮脏的解决方案,但易于设置,是对touch源文件进行修改,以便 Cargo 相信它们已更改:
$ cd /path/to/project/root
$ ls
Cargo.lock Cargo.toml src target
$ cargo build
Compiling abc v0.1.0 (file:///private/tmp/b/abc)
src/main.rs:2:9: 2:10 warning: unused variable: `x`, #[warn(unused_variables)] on by default
src/main.rs:2 let x: u8 = 123;
^
$ cargo build
$ touch $(find src)
$ cargo build
Compiling abc v0.1.0 (file:///private/tmp/b/abc)
src/main.rs:2:9: 2:10 warning: unused variable: `x`, #[warn(unused_variables)] on by default
src/main.rs:2 let x: u8 = 123;
^
Run Code Online (Sandbox Code Playgroud)
另一个可能更好的解决方案是清除target包含二进制工件的目录,方法是cargo clean:
$ cargo build
Compiling abc v0.1.0 (file:///private/tmp/b/abc)
src/main.rs:2:9: 2:10 warning: unused variable: `x`, #[warn(unused_variables)] on by default
src/main.rs:2 let x: u8 = 123;
^
$ cargo build
$ cargo clean
$ cargo build
Compiling abc v0.1.0 (file:///private/tmp/b/abc)
src/main.rs:2:9: 2:10 warning: unused variable: `x`, #[warn(unused_variables)] on by default
src/main.rs:2 let x: u8 = 123;
^
Run Code Online (Sandbox Code Playgroud)
它的优点是不会触发 Vim“文件已更改!” 警告,并且还可以在项目目录中的任何位置运行,而不仅仅是根目录。