如何在 Rust 中使用 CommandExt 方法

问题描述

在 Fedora 33 上运行 Rust

stable-x86_64-unkNown-linux-gnu (default)
rustc 1.51.0 (2fd73fabe 2021-03-23)

我正在尝试使用 https://doc.rust-lang.org/std/os/unix/process/trait.CommandExt.html 中的一些方法。是一个Trait,已经被Command结构体实现了,但是编译器找不到对应的方法

        let mut cmd = Command::new(target)
            .args(args);
        cmd.pre_exec(|| {});

错误

40 |         cmd.pre_exec(|| {});
   |             ^^^^^^^^ method not found in `&mut Command`

我怀疑我需要cfg target_os 来指示编译器?

解决方法

需要显式导入特征: use std::os::unix::process::CommandExt;。 这是必需的,因为 CommandExt 不是 https://doc.rust-lang.org/std/prelude/index.html

的一部分