From in std::convert - Rust (original) (raw)

pub trait From<T>: Sized {
    // Required method
    fn from(value: T) -> Self;
}

Expand description

Used to do value-to-value conversions while consuming the input value. It is the reciprocal ofInto.

One should always prefer implementing From over Intobecause implementing From automatically provides one with an implementation of Intothanks to the blanket implementation in the standard library.

Only implement Into when targeting a version prior to Rust 1.41 and converting to a type outside the current crate.From was not able to do these types of conversions in earlier versions because of Rust’s orphaning rules. See Into for more details.

Prefer using Into over using From when specifying trait bounds on a generic function. This way, types that directly implement Into can be used as arguments as well.

The From trait is also very useful when performing error handling. When constructing a function that is capable of failing, the return type will generally be of the form Result<T, E>.From simplifies error handling by allowing a function to return a single error type that encapsulates multiple error types. See the “Examples” section and the book for more details.

Note: This trait must not fail. The From trait is intended for perfect conversions. If the conversion can fail or is not perfect, use TryFrom.

§Generic Implementations

§When to implement From

While there’s no technical restrictions on which conversions can be done using a From implementation, the general expectation is that the conversions should typically be restricted as follows:

§Examples

String implements From<&str>:

An explicit conversion from a &str to a String is done as follows:

let string = "hello".to_string();
let other_string = String::from("hello");

assert_eq!(string, other_string);

While performing error handling it is often useful to implement From for your own error type. By converting underlying error types to our own custom error type that encapsulates the underlying error type, we can return a single error type without losing information on the underlying cause. The ‘?’ operator automatically converts the underlying error type to our custom error type with From::from.

use std::fs;
use std::io;
use std::num;

enum CliError {
    IoError(io::Error),
    ParseError(num::ParseIntError),
}

impl From<io::Error> for CliError {
    fn from(error: io::Error) -> Self {
        CliError::IoError(error)
    }
}

impl From<num::ParseIntError> for CliError {
    fn from(error: num::ParseIntError) -> Self {
        CliError::ParseError(error)
    }
}

fn open_and_parse_file(file_name: &str) -> Result<i32, CliError> {
    let mut contents = fs::read_to_string(&file_name)?;
    let num: i32 = contents.trim().parse()?;
    Ok(num)
}

1.0.0 · Source

Converts to this type from the input type.

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

1.17.0 · Source§

1.21.0 · Source§

1.0.0 · Source§

1.21.0 · Source§

1.0.0 · Source§

1.17.0 · Source§

1.7.0 · Source§

1.24.0 · Source§

1.24.0 · Source§

1.17.0 · Source§

1.24.0 · Source§

1.24.0 · Source§

1.17.0 · Source§

1.24.0 · Source§

1.24.0 · Source§

1.35.0 · Source§

1.84.0 · Source§

1.84.0 · Source§

1.44.0 · Source§

1.84.0 · Source§

1.84.0 · Source§

1.84.0 · Source§

1.84.0 · Source§

1.84.0 · Source§

1.84.0 · Source§

1.84.0 · Source§

1.84.0 · Source§

1.84.0 · Source§

1.84.0 · Source§

Source§

Source§

Source§

Source§

Source§

Source§

1.45.0 · Source§

1.45.0 · Source§

1.45.0 · Source§

1.45.0 · Source§

Source§

1.14.0 · Source§

Intended for use for errors not exposed to the user, where allocating onto the heap (for normal construction via Error::new) is too costly.

1.36.0 · Source§

1.34.0 · Source§

1.68.0 · Source§

1.68.0 · Source§

1.28.0 · Source§

1.28.0 · Source§

1.28.0 · Source§

1.28.0 · Source§

1.28.0 · Source§

1.28.0 · Source§

1.28.0 · Source§

1.28.0 · Source§

1.28.0 · Source§

1.28.0 · Source§

1.28.0 · Source§

1.28.0 · Source§

1.24.0 · Source§

1.13.0 · Source§

1.51.0 · Source§

1.51.0 · Source§

1.46.0 · Source§

1.6.0 · Source§

1.6.0 · Source§

1.6.0 · Source§

1.6.0 · Source§

1.6.0 · Source§

1.6.0 · Source§

1.6.0 · Source§

1.5.0 · Source§

1.5.0 · Source§

1.5.0 · Source§

1.26.0 · Source§

1.5.0 · Source§

1.34.0 · Source§

1.6.0 · Source§

1.6.0 · Source§

1.5.0 · Source§

1.5.0 · Source§

1.26.0 · Source§

1.26.0 · Source§

1.34.0 · Source§

1.6.0 · Source§

1.5.0 · Source§

1.26.0 · Source§

1.34.0 · Source§

1.26.0 · Source§

1.34.0 · Source§

1.23.0 · Source§

1.34.0 · Source§

Source§

1.13.0 · Source§

Maps a byte in 0x00..=0xFF to a char whose code point has the same value, in U+0000..=U+00FF.

Unicode is designed such that this effectively decodes bytes with the character encoding that IANA calls ISO-8859-1. This encoding is compatible with ASCII.

Note that this is different from ISO/IEC 8859-1 a.k.a. ISO 8859-1 (with one less hyphen), which leaves some “blanks”, byte values that are not assigned to any character. ISO-8859-1 (the IANA one) assigns them to the C0 and C1 control codes.

Note that this is also different from Windows-1252 a.k.a. code page 1252, which is a superset ISO/IEC 8859-1 that assigns some (not all!) blanks to punctuation and various Latin characters.

To confuse things further, on the Web ascii, iso-8859-1, and windows-1252 are all aliases for a superset of Windows-1252 that fills the remaining blanks with corresponding C0 and C1 control codes.

1.6.0 · Source§

1.6.0 · Source§

1.5.0 · Source§

1.5.0 · Source§

1.5.0 · Source§

1.26.0 · Source§

1.26.0 · Source§

1.5.0 · Source§

1.5.0 · Source§

1.5.0 · Source§

1.26.0 · Source§

1.5.0 · Source§

1.61.0 · Source§

1.34.0 · Source§

1.6.0 · Source§

1.6.0 · Source§

1.5.0 · Source§

1.5.0 · Source§

1.26.0 · Source§

1.5.0 · Source§

1.5.0 · Source§

1.26.0 · Source§

1.26.0 · Source§

1.34.0 · Source§

1.6.0 · Source§

1.5.0 · Source§

1.26.0 · Source§

1.5.0 · Source§

1.26.0 · Source§

1.1.0 · Source§

1.34.0 · Source§

1.26.0 · Source§

1.26.0 · Source§

1.34.0 · Source§

1.26.0 · Source§

1.23.0 · Source§

Source§

Source§

Source§

Source§

Source§

Source§

Source§

Source§

Source§

Source§

Source§

Source§

Source§

Source§

Source§

Source§

Source§

Source§

Source§

Source§

Source§

Source§

Source§

Source§

Source§

Source§

Source§

Source§

Source§

Source§

Source§

Source§

Source§

Source§

Source§

Source§

Source§

1.18.0 · Source§

Source§

1.18.0 · Source§

1.18.0 · Source§

1.18.0 · Source§

Source§

Source§

1.78.0 · Source§

1.20.0 · Source§

1.24.0 · Source§

1.24.0 · Source§

1.7.0 · Source§

1.0.0 · Source§

1.20.0 · Source§

1.0.0 · Source§

1.24.0 · Source§

1.24.0 · Source§

1.63.0 · Source§

1.63.0 · Source§

Available on Windows only.

1.20.0 · Source§

Source§

Source§

Source§

Source§

1.74.0 · Source§

1.74.0 · Source§

1.16.0 · Source§

1.1.0 · Source§

1.16.0 · Source§

1.26.0 · Source§

1.16.0 · Source§

1.16.0 · Source§

1.63.0 · Source§

1.63.0 · Source§

Available on Windows only.

1.63.0 · Source§

1.63.0 · Source§

Available on Windows only.

1.63.0 · Source§

1.63.0 · Source§

Available on Windows only.

1.41.0 · Source§

1.41.0 · Source§

1.41.0 · Source§

1.41.0 · Source§

1.41.0 · Source§

1.41.0 · Source§

1.41.0 · Source§

1.41.0 · Source§

1.41.0 · Source§

1.41.0 · Source§

1.41.0 · Source§

1.41.0 · Source§

1.41.0 · Source§

1.41.0 · Source§

1.41.0 · Source§

1.41.0 · Source§

1.41.0 · Source§

1.41.0 · Source§

1.41.0 · Source§

1.41.0 · Source§

1.41.0 · Source§

1.41.0 · Source§

1.41.0 · Source§

1.41.0 · Source§

1.41.0 · Source§

1.41.0 · Source§

1.41.0 · Source§

1.41.0 · Source§

1.41.0 · Source§

1.41.0 · Source§

1.41.0 · Source§

1.41.0 · Source§

1.41.0 · Source§

1.41.0 · Source§

1.41.0 · Source§

1.63.0 · Source§

Source§

Source§

1.63.0 · Source§

1.63.0 · Source§

1.63.0 · Source§

Source§

Available on Linux only.

1.63.0 · Source§

Available on Unix only.

1.63.0 · Source§

Available on Unix only.

1.63.0 · Source§

Available on Unix only.

1.74.0 · Source§

Available on Unix only.

Creates a ChildStderr from the provided OwnedFd.

The provided file descriptor must point to a pipe with the CLOEXEC flag set.

1.74.0 · Source§

Available on Unix only.

Creates a ChildStdin from the provided OwnedFd.

The provided file descriptor must point to a pipe with the CLOEXEC flag set.

1.74.0 · Source§

Available on Unix only.

Creates a ChildStdout from the provided OwnedFd.

The provided file descriptor must point to a pipe with the CLOEXEC flag set.

1.63.0 · Source§

Available on Unix only.

Source§

Available on Linux only.

1.63.0 · Source§

Available on Unix only.

1.63.0 · Source§

Available on Unix only.

1.63.0 · Source§

Available on Unix only.

1.63.0 · Source§

Available on Windows only.

1.74.0 · Source§

Available on Windows only.

Creates a ChildStderr from the provided OwnedHandle.

The provided handle must be asynchronous, as reading and writing from and to it is implemented using asynchronous APIs.

1.74.0 · Source§

Available on Windows only.

Creates a ChildStdin from the provided OwnedHandle.

The provided handle must be asynchronous, as reading and writing from and to it is implemented using asynchronous APIs.

1.74.0 · Source§

Available on Windows only.

Creates a ChildStdout from the provided OwnedHandle.

The provided handle must be asynchronous, as reading and writing from and to it is implemented using asynchronous APIs.

1.63.0 · Source§

Available on Windows only.

1.63.0 · Source§

Available on Windows only.

1.63.0 · Source§

Available on Windows only.

1.63.0 · Source§

Available on Windows only.

1.20.0 · Source§

1.14.0 · Source§

1.24.0 · Source§

1.24.0 · Source§

1.63.0 · Source§

Available on Windows only.

1.63.0 · Source§

Available on Unix only.

1.63.0 · Source§

Available on Windows only.

1.20.0 · Source§

1.63.0 · Source§

Available on Unix only.

1.63.0 · Source§

Available on Windows only.

1.20.0 · Source§

1.63.0 · Source§

Available on Unix only.

1.63.0 · Source§

Available on Windows only.

1.20.0 · Source§

Source§

Source§

Source§

1.62.0 · Source§

Source§

Source§

Source§

Source§

Source§

Source§

Source§

Source§

Source§

Source§

Source§

Source§

Source§

Source§

Source§

Source§

Source§

Source§

Source§

Source§

Source§

Source§

Source§

Source§

Source§

Source§

Source§

Source§

Source§

Source§

Source§

Source§

Source§

Source§

Source§

Source§

Source§

Source§

1.20.0 · Source§

1.0.0 · Source§

1.0.0 · Source§

1.21.0 · Source§

1.21.0 · Source§

1.14.0 · Source§

1.24.0 · Source§

1.24.0 · Source§

1.62.0 · Source§

Source§

Source§

1.43.0 · Source§

1.17.0 · Source§

1.9.0 · Source§

1.17.0 · Source§

1.9.0 · Source§

1.17.0 · Source§

1.16.0 · Source§

1.0.0 · Source§

Source§

Source§

Source§

1.28.0 · Source§

1.28.0 · Source§

1.28.0 · Source§

1.28.0 · Source§

1.6.0 · Source§

1.28.0 · Source§

1.28.0 · Source§

1.6.0 · Source§

1.0.0 · Source§

1.14.0 · Source§

1.28.0 · Source§

1.28.0 · Source§

1.28.0 · Source§

Source§

1.28.0 · Source§

1.28.0 · Source§

1.6.0 · Source§

1.0.0 · Source§

1.6.0 · Source§

1.0.0 · Source§

1.22.0 · Source§

1.22.0 · Source§

1.45.0 · Source§

1.45.0 · Source§

1.0.0 · Source§

1.0.0 · Source§

1.30.0 · Source§

1.8.0 · Source§

1.28.0 · Source§

1.30.0 · Source§

1.14.0 · Source§

1.8.0 · Source§

1.77.0 · Source§

Source§

Creates a new BorrowedBuf from a fully initialized slice.

Source§

Creates a new BorrowedBuf from an uninitialized buffer.

Use set_init if part of the buffer is known to be already initialized.

1.19.0 · Source§

Source§

1.17.0 · Source§

1.56.0 · Source§

1.56.0 · Source§

1.17.0 · Source§

1.21.0 · Source§

1.21.0 · Source§

1.0.0 · Source§

1.84.0 · Source§

1.84.0 · Source§

1.84.0 · Source§

1.19.0 · Source§

1.45.0 · Source§

1.71.0 · Source§

This trait is implemented for tuples up to twelve items long.

1.34.0 · Source§

Stability note: This impl does not yet exist, but we are “reserving space” to add it in the future. Seerust-lang/rust#64715 for details.

1.23.0 · Source§

1.25.0 · Source§

1.25.0 · Source§

1.71.0 · Source§

This trait is implemented for tuples up to twelve items long.

1.31.0 · Source§

Source§

Source§

Source§

Source§

Source§

Source§

Source§

1.24.0 · Source§

1.0.0 · Source§

1.63.0 · Source§

Available on Windows only.

1.12.0 · Source§

1.36.0 · Source§

1.6.0 · Source§

1.12.0 · Source§

1.70.0 · Source§

1.12.0 · Source§

Source§

1.12.0 · Source§

1.6.0 · Source§

1.6.0 · Source§

Source§

1.24.0 · Source§

1.70.0 · Source§

Source§

1.24.0 · Source§

1.0.0 · Source§

1.18.0 · Source§

1.33.0 · Source§

1.21.0 · Source§

1.21.0 · Source§

1.5.0 · Source§

1.10.0 · Source§

1.20.0 · Source§

1.5.0 · Source§

1.10.0 · Source§

1.21.0 · Source§

1.21.0 · Source§

1.74.0 · Source§

1.74.0 · Source§

1.45.0 · Source§

1.56.0 · Source§

1.56.0 · Source§

1.56.0 · Source§

1.56.0 · Source§

1.56.0 · Source§

1.74.0 · Source§

Source§

1.74.0 · Source§

1.44.0 · Source§

Source§

Source§

Source§

Source§

1.0.0 · Source§

1.0.0 · Source§

1.0.0 · Source§

Source§

Source§

1.51.0 · Source§

1.51.0 · Source§

Source§

Source§

Source§

Source§

Source§

Source§

Source§

Source§

Source§

Source§

Source§

Source§

Source§

Source§

Source§

Source§

Source§

Source§

Source§

Source§