【问题标题】:Rust: invoking rustc using subprocess shellRust:使用子进程 shell 调用 rustc
【发布时间】:2021-04-16 07:49:24
【问题描述】:

所以问题来了:我想在一段代码上调用一个 rust 编译器。它不是通过 stderr 发出警告,而是在 stdout 上输出帮助页面并退出。这有什么问题?

let compilation = subprocess::Exec::shell("rustc")
.arg("-o").arg("binary")
.arg("-O").arg(path)
.stderr(subprocess::Redirection::Merge)
.capture().unwrap();

let compilationpassed = compilation.success();
let compilationlog = compilation.stdout_str();

【问题讨论】:

  • 我不知道您使用的库,但如果您不知道,您可以使用标准库 (process::Command) 非常简单地做到这一点。
  • 啊哥们。我正在使用子流程箱。你能告诉我如何用std来做吗?请问?
  • 我有this example,其中存储了 rust 编译的输出以供分析,但这是一个具有并行性和中断的相当复杂的示例。您可能会发现 process::Command 文档对介绍更有帮助。
  • 你是完全正确的。 process::Command 可以满足我的所有需求。
  • 如果您有固定且优雅的代码,建议您将其粘贴为带有解释和 cmets 的答案。这样的例子可以帮助以后遇到类似问题的用户。

标签: shell rust subprocess


【解决方案1】:

您不需要特定的 crate 来启动进程,标准的 process::Command 包含您需要的内容。

你似乎想启动 rustc 然后继承标准输入和/或标准输出。

应该是这样的:

std::process::Command::new("rustc")
    .arg("-o").arg("binary")
    .arg("-O").arg(path)
    .spawn()
    .expect("ls command failed to start")
    .wait(); // you should better handle the result here

如果您想丢弃 stderr 或 stdout,您可以使用同音函数 stderrstdout 更改行为。

【讨论】:

    猜你喜欢
    • 2017-04-08
    • 2014-10-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-09-26
    • 2020-04-22
    • 1970-01-01
    相关资源
    最近更新 更多