运行 `rustc` 来检查程序而不生成任何文件 [英] Run `rustc` to check a program without generating any files

查看:37
本文介绍了运行 `rustc` 来检查程序而不生成任何文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

正如 cargo check 所示,在不实际生成代码的情况下检查您的程序是否格式正确通常很有用(通常是一项非常耗时的任务).我想直接使用 rustc 检查单个(库)Rust 文件(我不能使用 Cargo!).

As cargo check shows, it's often useful to check if your program is well-formed without actually generating code (an often pretty time-consuming task). I want to check a single (library) Rust file with rustc directly (I cannot use Cargo!).

cargo check 显然是这样调用的:

rustc --emit=metadata -Z no-codegen

这只会发出元数据,一个 .rmeta 文件.Cargo 实际上需要根据检查的箱子检查箱子.就我而言,我真的不需要元数据文件.

This only emits metadata, a .rmeta file. Cargo actually needs that to check crates dependent on the checked crate. In my case I really don't need the metadata file.

我尝试了以下方法:

rustc --crate-type=lib --emit=
rustc --crate-type=lib --emit=nothing

但两者都不起作用.我使用 --crate-type=lib 因为我的文件没有 main 函数.我需要一个独立于平台的解决方案(我不只是想在我的机器上使用它,而是在公共脚本中使用它).

But both didn't work. I use --crate-type=lib because my file doesn't have a main function. I need a platform-independent solution (I don't just want to use it on my machine, but use it in a public script).

如何让 rustc 不写单个文件?

How do I make rustc not write a single file?

推荐答案

引用 我自己的 GitHub 评论关于这个问题,稳定 Rust 有几个选项:

To quote my own GitHub comment about this very question, there are a few options for stable Rust:

  • rustc --emit=mir -o/dev/null 似乎在 1.18 及更新版本中工作,什么也不写.(--emit=mir 是唯一有用的 --emit 选项——其他选项试图创建像 /dev/null.foo0.rcgu.o 这样愚蠢的文件,除了 --emit=dep-info,它不做检查.)
  • rustc -C extra-filename=-tmp -C linker=true(即使用 /bin/true 作为链接器")似乎适用于所有版本,编写一些中间文件但清理它们.
  • rustc --out-dir=<新的空临时目录> 不太聪明,因此可能不太可能破坏?
  • rustc --emit=mir -o /dev/null seems to work in 1.18 and newer, writing nothing. (--emit=mir is the only helpful --emit option—the others try to create silly files like /dev/null.foo0.rcgu.o, except --emit=dep-info, which does no checking.)
  • rustc -C extra-filename=-tmp -C linker=true (i.e. use /bin/true as a "linker") seems to work in all versions, writing some intermediate files but cleaning them up.
  • rustc --out-dir=<new empty temporary directory> is less clever and therefore perhaps less likely to break?

请注意,链接器错误(如果有)不会被前两个选项发现(也不会被每晚使用的 -Zno-codegen 选项发现).

Note that linker errors, if any, will not be found by the first two options (nor by the nightly-only -Zno-codegen option).

这篇关于运行 `rustc` 来检查程序而不生成任何文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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