Trait core::convert::From1.0.0[][src]

pub trait From<T>: Sized {
    fn from(_: T) -> Self;
}
Expand description

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

One should always prefer implementing From over Into because implementing From automatically provides one with an implementation of Into thanks 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 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>. The From trait simplifies error handling by allowing a function to return a single error type that encapsulate multiple error types. See the “Examples” section and the book for more details.

Note: This trait must not fail. If the conversion can fail, use TryFrom.

Generic Implementations

  • From<T> for U implies Into<U> for T
  • From is reflexive, which means that From<T> for T is implemented

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);
Run

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 by calling Into<CliError>::into which is automatically provided when implementing From. The compiler then infers which implementation of Into should be used.

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)
}
Run

Required methods

Performs the conversion.

Implementors

Converts NonZeroI8 to NonZeroI16 losslessly.

Converts NonZeroI8 to NonZeroI32 losslessly.

Converts NonZeroI8 to NonZeroI64 losslessly.

Converts NonZeroI8 to NonZeroI128 losslessly.

Converts NonZeroI8 to NonZeroIsize losslessly.

Converts a NonZeroI8 into an i8

Converts NonZeroI16 to NonZeroI32 losslessly.

Converts NonZeroI16 to NonZeroI64 losslessly.

Converts NonZeroI16 to NonZeroI128 losslessly.

Converts NonZeroI16 to NonZeroIsize losslessly.

Converts a NonZeroI16 into an i16

Converts NonZeroI32 to NonZeroI64 losslessly.

Converts NonZeroI32 to NonZeroI128 losslessly.

Converts a NonZeroI32 into an i32

Converts NonZeroI64 to NonZeroI128 losslessly.

Converts a NonZeroI64 into an i64

Converts a NonZeroI128 into an i128

Converts a NonZeroIsize into an isize

Converts NonZeroU8 to NonZeroI16 losslessly.

Converts NonZeroU8 to NonZeroI32 losslessly.

Converts NonZeroU8 to NonZeroI64 losslessly.

Converts NonZeroU8 to NonZeroI128 losslessly.

Converts NonZeroU8 to NonZeroIsize losslessly.

Converts NonZeroU8 to NonZeroU16 losslessly.

Converts NonZeroU8 to NonZeroU32 losslessly.

Converts NonZeroU8 to NonZeroU64 losslessly.

Converts NonZeroU8 to NonZeroU128 losslessly.

Converts NonZeroU8 to NonZeroUsize losslessly.

Converts a NonZeroU8 into an u8

Converts NonZeroU16 to NonZeroI32 losslessly.

Converts NonZeroU16 to NonZeroI64 losslessly.

Converts NonZeroU16 to NonZeroI128 losslessly.

Converts NonZeroU16 to NonZeroU32 losslessly.

Converts NonZeroU16 to NonZeroU64 losslessly.

Converts NonZeroU16 to NonZeroU128 losslessly.

Converts NonZeroU16 to NonZeroUsize losslessly.

Converts a NonZeroU16 into an u16

Converts NonZeroU32 to NonZeroI64 losslessly.

Converts NonZeroU32 to NonZeroI128 losslessly.

Converts NonZeroU32 to NonZeroU64 losslessly.

Converts NonZeroU32 to NonZeroU128 losslessly.

Converts a NonZeroU32 into an u32

Converts NonZeroU64 to NonZeroI128 losslessly.

Converts NonZeroU64 to NonZeroU128 losslessly.

Converts a NonZeroU64 into an u64

Converts a NonZeroU128 into an u128

Converts a NonZeroUsize into an usize

Converts a bool into an AtomicBool.

Examples

use std::sync::atomic::AtomicBool;
let atomic_bool = AtomicBool::from(true);
assert_eq!(format!("{:?}", atomic_bool), "true")
Run

Converts a bool to a i8. The resulting value is 0 for false and 1 for true values.

Examples

assert_eq!(i8::from(true), 1);
assert_eq!(i8::from(false), 0);
Run

Converts a bool to a i16. The resulting value is 0 for false and 1 for true values.

Examples

assert_eq!(i16::from(true), 1);
assert_eq!(i16::from(false), 0);
Run

Converts a bool to a i32. The resulting value is 0 for false and 1 for true values.

Examples

assert_eq!(i32::from(true), 1);
assert_eq!(i32::from(false), 0);
Run

Converts a bool to a i64. The resulting value is 0 for false and 1 for true values.

Examples

assert_eq!(i64::from(true), 1);
assert_eq!(i64::from(false), 0);
Run

Converts a bool to a i128. The resulting value is 0 for false and 1 for true values.

Examples

assert_eq!(i128::from(true), 1);
assert_eq!(i128::from(false), 0);
Run

Converts a bool to a isize. The resulting value is 0 for false and 1 for true values.

Examples

assert_eq!(isize::from(true), 1);
assert_eq!(isize::from(false), 0);
Run

Converts a bool to a u8. The resulting value is 0 for false and 1 for true values.

Examples

assert_eq!(u8::from(true), 1);
assert_eq!(u8::from(false), 0);
Run

Converts a bool to a u16. The resulting value is 0 for false and 1 for true values.

Examples

assert_eq!(u16::from(true), 1);
assert_eq!(u16::from(false), 0);
Run

Converts a bool to a u32. The resulting value is 0 for false and 1 for true values.

Examples

assert_eq!(u32::from(true), 1);
assert_eq!(u32::from(false), 0);
Run

Converts a bool to a u64. The resulting value is 0 for false and 1 for true values.

Examples

assert_eq!(u64::from(true), 1);
assert_eq!(u64::from(false), 0);
Run

Converts a bool to a u128. The resulting value is 0 for false and 1 for true values.

Examples

assert_eq!(u128::from(true), 1);
assert_eq!(u128::from(false), 0);
Run

Converts a bool to a usize. The resulting value is 0 for false and 1 for true values.

Examples

assert_eq!(usize::from(true), 1);
assert_eq!(usize::from(false), 0);
Run

Converts a char into a u32.

Examples

use std::mem;

let c = 'c';
let u = u32::from(c);
assert!(4 == mem::size_of_val(&u))
Run

Converts a char into a u64.

Examples

use std::mem;

let c = '👤';
let u = u64::from(c);
assert!(8 == mem::size_of_val(&u))
Run

Converts a char into a u128.

Examples

use std::mem;

let c = '⚙';
let u = u128::from(c);
assert!(16 == mem::size_of_val(&u))
Run

Converts f32 to f64 losslessly.

Converts an i8 into an AtomicI8.

Converts i8 to f32 losslessly.

Converts i8 to f64 losslessly.

Converts i8 to i16 losslessly.

Converts i8 to i32 losslessly.

Converts i8 to i64 losslessly.

Converts i8 to i128 losslessly.

Converts i8 to isize losslessly.

Converts an i16 into an AtomicI16.

Converts i16 to f32 losslessly.

Converts i16 to f64 losslessly.

Converts i16 to i32 losslessly.

Converts i16 to i64 losslessly.

Converts i16 to i128 losslessly.

Converts i16 to isize losslessly.

Converts an i32 into an AtomicI32.

Converts i32 to f64 losslessly.

Converts i32 to i64 losslessly.

Converts i32 to i128 losslessly.

Converts an i64 into an AtomicI64.

Converts i64 to i128 losslessly.

Converts an isize into an AtomicIsize.

Converts an u8 into an AtomicU8.

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.

Converts a u8 into a char.

Examples

use std::mem;

let u = 32 as u8;
let c = char::from(u);
assert!(4 == mem::size_of_val(&c))
Run

Converts u8 to f32 losslessly.

Converts u8 to f64 losslessly.

Converts u8 to i16 losslessly.

Converts u8 to i32 losslessly.

Converts u8 to i64 losslessly.

Converts u8 to i128 losslessly.

Converts u8 to isize losslessly.

Converts u8 to u16 losslessly.

Converts u8 to u32 losslessly.

Converts u8 to u64 losslessly.

Converts u8 to u128 losslessly.

Converts u8 to usize losslessly.

Converts an u16 into an AtomicU16.

Converts u16 to f32 losslessly.

Converts u16 to f64 losslessly.

Converts u16 to i32 losslessly.

Converts u16 to i64 losslessly.

Converts u16 to i128 losslessly.

Converts u16 to u32 losslessly.

Converts u16 to u64 losslessly.

Converts u16 to u128 losslessly.

Converts u16 to usize losslessly.

Converts an u32 into an AtomicU32.

Converts u32 to f64 losslessly.

Converts u32 to i64 losslessly.

Converts u32 to i128 losslessly.

Converts u32 to u64 losslessly.

Converts u32 to u128 losslessly.

Converts an u64 into an AtomicU64.

Converts u64 to i128 losslessly.

Converts u64 to u128 losslessly.

Converts an usize into an AtomicUsize.

Converts from &Option<T> to Option<&T>.

Examples

Converts an Option<String> into an Option<usize>, preserving the original. The map method takes the self argument by value, consuming the original, so this technique uses as_ref to first take an Option to a reference to the value inside the original.

let s: Option<String> = Some(String::from("Hello, Rustaceans!"));
let o: Option<usize> = Option::from(&s).map(|ss: &String| ss.len());

println!("Can still print s: {:?}", s);

assert_eq!(o, Some(18));
Run

Converts from &mut Option<T> to Option<&mut T>

Examples

let mut s = Some(String::from("Hello"));
let o: Option<&mut String> = Option::from(&mut s);

match o {
    Some(t) => *t = String::from("Hello, Rustaceans!"),
    None => (),
}

assert_eq!(s, Some(String::from("Hello, Rustaceans!")));
Run

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

Copies val into a new Some.

Examples

let o: Option<u8> = Option::from(67);

assert_eq!(Some(67), o);
Run

Convert to a Ready variant.

Example

assert_eq!(Poll::from(true), Poll::Ready(true));
Run