Struct std::process::ExitStatus

1.0.0 · source ·
pub struct ExitStatus(_);
Expand description

描述进程终止后的结果。

struct 用于表示子进程的退出状态或其他终止。 子进程是通过 Command 结构体创建的,其退出状态通过 status 方法或 Child 进程的 wait 方法公开。

ExitStatus 表示进程的每一种可能的配置。在 Unix 上,这是等待状态。不是 仅仅是 exit 状态(传递给 exit 的值)。

为了正确报告失败进程的错误,请使用 Display 的实现打印 ExitStatus 或 ExitStatusError 的值。

与 ExitCode 的区别

ExitCode 旨在通过 Termination trait 终止当前正在运行的进程,而 ExitStatus 则表示终止子进程。 由于平台兼容性差异及其预期用途,这些 API 是分开的; 事后通常不可能从子节点那里准确地复制当前进程的 ExitStatus。

Implementations§

source§

impl ExitStatus

source

pub fn exit_ok(&self) -> Result<(), ExitStatusError>

🔬This is a nightly-only experimental API. (exit_status_error #84908)

终止成功了吗? 返回 Result。

Examples
#![feature(exit_status_error)]
use std::process::Command;

let status = Command::new("ls")
                     .arg("/dev/nonexistent")
                     .status()
                     .expect("ls could not be executed");

println!("ls: {status}");
status.exit_ok().expect_err("/dev/nonexistent could be listed!");
Run
source

pub fn success(&self) -> bool

终止成功了吗? 信号终止不被视为成功,并且成功被定义为零退出状态。

Examples
use std::process::Command;

let status = Command::new("mkdir")
                     .arg("projects")
                     .status()
                     .expect("failed to execute mkdir");

if status.success() {
    println!("'projects/' directory created");
} else {
    println!("failed to create 'projects/' directory: {status}");
}
Run
source

pub fn code(&self) -> Option<i32>

返回进程的退出代码 (如果有)。

用 Unix 的术语来说,返回值是退出状态:如果进程通过调用 exit 完成,则传递给 exit 的值。 请注意,在 Unix 上,退出状态被截断为 8 位,并且不是来自程序调用到 exit 的值可能是由运行时系统发明的 (通常,例如,255、254、127 或 126)。

在 Unix 上,如果进程被信号终止,它将返回 None。 ExitStatusExt 是 trait 的扩展,用于从 ExitStatus 中提取任何此类信号和其他细节。

Examples
use std::process::Command;

let status = Command::new("mkdir")
                     .arg("projects")
                     .status()
                     .expect("failed to execute mkdir");

match status.code() {
    Some(code) => println!("Exited with status code: {code}"),
    None       => println!("Process terminated by signal")
}
Run

Trait Implementations§

source§

impl Clone for ExitStatus

source§

fn clone(&self) -> ExitStatus

返回值的副本。 Read more
source§

fn clone_from(&mut self, source: &Self)

从 source 执行复制分配。 Read more
source§

impl Debug for ExitStatus

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

使用给定的格式化程序格式化该值。 Read more
source§

impl Display for ExitStatus

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

使用给定的格式化程序格式化该值。 Read more
source§

impl ExitStatusExt for ExitStatus

Available on Unix only.
source§

fn from_raw(raw: i32) -> Self

从 wait 的原始底层整数状态值创建一个新的 ExitStatus 或 ExitStatusError Read more
source§

fn signal(&self) -> Option<i32>

如果进程被一个信号终止,则返回该信号。 Read more
source§

fn core_dumped(&self) -> bool

如果进程被一个信号终止,说明它是否丢弃了核心。
source§

fn stopped_signal(&self) -> Option<i32>

如果该进程被信号停止,则返回该信号。 Read more
source§

fn continued(&self) -> bool

进程是否从停止状态继续。 Read more
source§

fn into_raw(self) -> i32

返回底层的原始 wait 状态。 Read more
1.12.0 · source§

impl ExitStatusExt for ExitStatus

Available on Windows only.
source§

fn from_raw(raw: u32) -> Self

根据进程的原始底层 u32 返回值创建新的 ExitStatus。
source§

impl Into<ExitStatus> for ExitStatusError

source§

fn into(self) -> ExitStatus

将此类型转换为 (通常是推断的) 输入类型。
source§

impl PartialEq<ExitStatus> for ExitStatus

source§

fn eq(&self, other: &ExitStatus) -> bool

此方法测试 self 和 other 值是否相等,并由 == 使用。
source§

fn ne(&self, other: &Rhs) -> bool

此方法测试 !=。 默认实现几乎总是足够的,并且不应在没有充分理由的情况下被覆盖。
source§

impl Copy for ExitStatus

source§

impl Eq for ExitStatus

source§

impl StructuralEq for ExitStatus

source§

impl StructuralPartialEq for ExitStatus

Auto Trait Implementations§

Blanket Implementations§

source§

impl<T> Any for Twhere T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

获取 self 的 TypeId。 Read more
source§

impl<T> Borrow<T> for Twhere T: ?Sized,

source§

fn borrow(&self) -> &T

从拥有的值中一成不变地借用。 Read more
source§

impl<T> BorrowMut<T> for Twhere T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

从拥有的值中借用。 Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

返回未更改的参数。

source§

impl<T, U> Into<U> for Twhere U: From<T>,

source§

fn into(self) -> U

调用 U::from(self)。

也就是说,这种转换是 From<T> for U 实现选择执行的任何操作。

source§

impl<T> ToOwned for Twhere T: Clone,

§

type Owned = T

获得所有权后的结果类型。
source§

fn to_owned(&self) -> T

从借用的数据创建拥有的数据,通常是通过克隆。 Read more
source§

fn clone_into(&self, target: &mut T)

使用借来的数据来替换拥有的数据,通常是通过克隆。 Read more
source§

impl<T> ToString for Twhere T: Display + ?Sized,

source§

default fn to_string(&self) -> String

将给定值转换为 String。 Read more
source§

impl<T, U> TryFrom<U> for Twhere U: Into<T>,

§

type Error = Infallible

发生转换错误时返回的类型。
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

执行转换。
source§

impl<T, U> TryInto<U> for Twhere U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

发生转换错误时返回的类型。
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

执行转换。