繁体   English   中英

货物 rust 构建脚本 - 打印命令 output

[英]cargo rust build script - print output of command

我是 rust 和 cargo 的新手,我正在尝试做一些非常简单的事情!

我有这样的东西(在 build.rs 中):

use std::process::Command;

fn main() {
    Command::new("echo 123");
}

我想查看命令echo 123的 output 。 我希望123打印到构建 output(这主要是为了调试我正在做的事情)并且不会成为最终项目的一部分。

我试过cargo build --verbose - 这不起作用。

我无法从那里的帖子(以及其他一些类似的帖子)中推断出答案:

我觉得这一定很简单 - 但我一直在寻找 web 几个小时,但没有找到答案。

仅使用Command::new构建Command还没有执行它。 它只是启动了构建器模式。 要实际执行它,您必须使用方法spawnoutputstatus 例子:

Command::new("echo")
    .arg("123")
    .spawn()
    .expect("failed to spawn process");

非常不幸的是,这不会产生警告。 最近有人尝试将#[must_use]属性添加到Command ,这会使您的代码产生警告。 PR 暂时关闭,但似乎最终会添加。

除了@Lukas Kalbertodt 的回答,我不得不将该行添加到我的 Cargo.toml

build = "build.rs"

它就像一个魅力:)

我们可以使用一个宏,它对我有效,但有一个警告,因为它使用货物来显示。 但这对我来说很好。 我从 git 中心讨论中找到了以下代码: Cargo doesn't display output from a command in build.rs #985

macro_rules! p {
    ($($tokens: tt)*) => {
        println!("cargo:warning={}", format!($($tokens)*))
    }
}

fn main() {
    p!("BUILD.rs -> Starting ...");
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM