rust - 是否可以让 Cargo 始终显示警告?

标签 rust rust-cargo

我将 watchcargo 一起使用,以便快速查看编译时错误。但是,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
$

这意味着在 watch 给我一个清晰的屏幕之前,我只能看到几秒钟的警告。

有没有办法让 cargo build 总是给我警告?

最佳答案

从 Rust 1.40 开始,Cargo will cache the compiler messages .这意味着即使代码不需要重新编译,之前的警告也会被打印出来。

使用rust 1.40

% 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

使用rust 1.39

% 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

关于rust - 是否可以让 Cargo 始终显示警告?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53878090/

相关文章:

rust - 如何构建具有相同配置选项的二进制文件和库?

rust - 为什么我可以在 Rust 中从向量的末尾开始切片?

rust - 为什么我不能在 par_iter 上调用 collect_into?

rust - 我如何在稳定的 Rust 中使用 std::collections::BitSet?

rust - 在构建脚本中获取当前版本的方法?

rust - 工作区在 Rust 中提供两种不同的行为

rust - 无法验证生成的可执行文件是否为带有 `cargo readobj` : no such subcommand 的 ARM 二进制文件

rust - 预期算术溢出,但不会发生

rust - 如何从C++填充Rust函数指针

rust - 如何从构建脚本(build.rs)访问当前 cargo 文件(构建,测试,基准,文档等)