Skip to content

Commit 0df02bb

Browse files
committed
Remove antipattern from process::exit docs
1 parent 8ff0fd1 commit 0df02bb

File tree

1 file changed

+13
-28
lines changed

1 file changed

+13
-28
lines changed

library/std/src/process.rs

Lines changed: 13 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1964,47 +1964,32 @@ impl Child {
19641964
/// process, no destructors on the current stack or any other thread's stack
19651965
/// will be run. If a clean shutdown is needed it is recommended to only call
19661966
/// this function at a known point where there are no more destructors left
1967-
/// to run.
1967+
/// to run; or, preferably, simply return a type implementing [`Termination`]
1968+
/// (such as [`ExitCode`] or `Result`) from the `main` function and avoid this
1969+
/// function altogether:
1970+
///
1971+
/// ```
1972+
/// # use std::io::Error as MyError;
1973+
/// fn main() -> Result<(), MyError> {
1974+
/// // ...
1975+
/// Ok(())
1976+
/// }
1977+
/// ```
19681978
///
19691979
/// ## Platform-specific behavior
19701980
///
19711981
/// **Unix**: On Unix-like platforms, it is unlikely that all 32 bits of `exit`
19721982
/// will be visible to a parent process inspecting the exit code. On most
19731983
/// Unix-like platforms, only the eight least-significant bits are considered.
19741984
///
1975-
/// # Examples
1976-
///
1977-
/// Due to this function’s behavior regarding destructors, a conventional way
1978-
/// to use the function is to extract the actual computation to another
1979-
/// function and compute the exit code from its return value:
1980-
///
1981-
/// ```
1982-
/// fn run_app() -> Result<(), ()> {
1983-
/// // Application logic here
1984-
/// Ok(())
1985-
/// }
1986-
///
1987-
/// fn main() {
1988-
/// std::process::exit(match run_app() {
1989-
/// Ok(_) => 0,
1990-
/// Err(err) => {
1991-
/// eprintln!("error: {err:?}");
1992-
/// 1
1993-
/// }
1994-
/// });
1995-
/// }
1996-
/// ```
1997-
///
1998-
/// Due to [platform-specific behavior], the exit code for this example will be
1999-
/// `0` on Linux, but `256` on Windows:
1985+
/// For example, the exit code for this example will be `0` on Linux, but `256`
1986+
/// on Windows:
20001987
///
20011988
/// ```no_run
20021989
/// use std::process;
20031990
///
20041991
/// process::exit(0x0100);
20051992
/// ```
2006-
///
2007-
/// [platform-specific behavior]: #platform-specific-behavior
20081993
#[stable(feature = "rust1", since = "1.0.0")]
20091994
pub fn exit(code: i32) -> ! {
20101995
crate::rt::cleanup();

0 commit comments

Comments
 (0)