是否可以让 Cargo 始终显示警告? [英] Is it possible to have Cargo always show warnings?

查看:124
本文介绍了是否可以让 Cargo 始终显示警告?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将 watchcargo 一起使用,以便快速查看编译时错误.但是,cargo build 只会在第一次构建时显示错误.

I'm using watch with cargo, in order to quickly see compile time errors. However, cargo build will only show errors when building the first time.

$ 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 给我一个清晰的屏幕之前,我只能看到几秒钟的警告.

Which means I only get to see the warnings for a few seconds before watch gives me a clear screen.

有什么办法可以让 cargo build 总是给我警告?

Is there any way to make cargo build always give me the warnings?

推荐答案

警告只会在 Rust 重新编译你的文件时发生;但是它会尽可能多地缓存,如果某些内容没有改变,它会很高兴地跳过无用的编译.Cargo 目前没有强制重建的选项.

Warnings only happen when Rust recompiles your files; however it caches as much as possible and if something hasn't changed it will happily skip an useless compile. There's currently no option in Cargo to force rebuild.

一个快速而肮脏但易于设置的解决方案是touch您的源文件,以便 Cargo 相信它们已更改:

A quick and dirty solution, but easy to setup, is to touch your source files so that Cargo believes they changed:

$ 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;
                        ^

另一种可能更好的解决方案是使用 cargo clean 清除包含二进制工件的 target 目录:

Another solution, maybe better, would be to clean off the target directory containing binary artifacts, with 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;
                      ^

它的优点是不会触发 Vim 的文件更改!"警告,也可以在项目目录中的任何位置运行,而不仅仅是根目录.

It has the advantage of not triggering Vim "file changed!" warnings and is also runnable anywhere within the project dir, not just the root.

这篇关于是否可以让 Cargo 始终显示警告?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆