mirror of
https://github.com/rust-lang/rust.git
synced 2025-02-16 17:03:35 +00:00
Rollup merge of #38518 - nagisa:exec-doc, r=alexcrichton
Expand documentation of process::exit and exec Show a conventional way to use process::exit when destructors are considered important and also mention that the same caveats wrt destructors apply to exec as well.
This commit is contained in:
commit
001bfb9e56
@ -959,10 +959,27 @@ impl Child {
|
||||
///
|
||||
/// # Examples
|
||||
///
|
||||
/// ```
|
||||
/// use std::process;
|
||||
/// Due to this function’s behavior regarding destructors, a conventional way
|
||||
/// to use the function is to extract the actual computation to another
|
||||
/// function and compute the exit code from its return value:
|
||||
///
|
||||
/// process::exit(0);
|
||||
/// ```
|
||||
/// use std::io::{self, Write};
|
||||
///
|
||||
/// fn run_app() -> Result<(), ()> {
|
||||
/// // Application logic here
|
||||
/// Ok(())
|
||||
/// }
|
||||
///
|
||||
/// fn main() {
|
||||
/// ::std::process::exit(match run_app() {
|
||||
/// Ok(_) => 0,
|
||||
/// Err(err) => {
|
||||
/// writeln!(io::stderr(), "error: {:?}", err).unwrap();
|
||||
/// 1
|
||||
/// }
|
||||
/// });
|
||||
/// }
|
||||
/// ```
|
||||
///
|
||||
/// Due to [platform-specific behavior], the exit code for this example will be
|
||||
|
@ -67,10 +67,20 @@ pub trait CommandExt {
|
||||
/// an error indicating why the exec (or another part of the setup of the
|
||||
/// `Command`) failed.
|
||||
///
|
||||
/// `exec` not returning has the same implications as calling
|
||||
/// [`process::exit`] – no destructors on the current stack or any other
|
||||
/// thread’s stack will be run. Therefore, it is recommended to only call
|
||||
/// `exec` at a point where it is fine to not run any destructors. Note,
|
||||
/// that the `execvp` syscall independently guarantees that all memory is
|
||||
/// freed and all file descriptors with the `CLOEXEC` option (set by default
|
||||
/// on all file descriptors opened by the standard library) are closed.
|
||||
///
|
||||
/// This function, unlike `spawn`, will **not** `fork` the process to create
|
||||
/// a new child. Like spawn, however, the default behavior for the stdio
|
||||
/// descriptors will be to inherited from the current process.
|
||||
///
|
||||
/// [`process::exit`]: ../../../process/fn.exit.html
|
||||
///
|
||||
/// # Notes
|
||||
///
|
||||
/// The process may be in a "broken state" if this function returns in
|
||||
|
Loading…
Reference in New Issue
Block a user