core::convert::From - Rust
Description: API documentation for the Rust `From` trait in crate `core`.
Keywords: rust, rustlang, rust-lang, From

# 1.0.0[−][src]Trait core::convert::From

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

Simple and safe type conversions in to `Self`. It is the reciprocal of `Into`.

This trait is useful when performing error handling as described by the book and is closely related to the `?` operator.

When constructing a function that is capable of failing the return type will generally be of the form `Result<T, E>`.

The `From` trait allows for simplification of error handling by providing a means of returning a single error type that encapsulates numerous possible erroneous situations.

This trait is not limited to error handling, rather the general case for this trait would be in any type conversions to have an explicit definition of how they are performed.

Note: this trait must not fail. If the conversion can fail, use `TryFrom` or a dedicated method which returns an `Option<T>` or a `Result<T, E>`.

# 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>`:

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

    assert_eq!(string, other_string);Run

An example usage for error handling:

    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

### `fn from(_: T) -> Self`

Performs the conversion.

Loading content...

## Implementors

### `impl From<!> for TryFromIntError`[src]

#### `fn from(never: !) -> TryFromIntError`[src]

### `impl From<NonZeroU128> for u128`[src]

#### `fn from(nonzero: NonZeroU128) -> Self`[src]

### `impl From<NonZeroU16> for u16`[src]

#### `fn from(nonzero: NonZeroU16) -> Self`[src]

### `impl From<NonZeroU32> for u32`[src]

#### `fn from(nonzero: NonZeroU32) -> Self`[src]

### `impl From<NonZeroU64> for u64`[src]

#### `fn from(nonzero: NonZeroU64) -> Self`[src]

### `impl From<NonZeroU8> for u8`[src]

#### `fn from(nonzero: NonZeroU8) -> Self`[src]

### `impl From<NonZeroUsize> for usize`[src]

#### `fn from(nonzero: NonZeroUsize) -> Self`[src]

### `impl From<LocalWaker> for Waker`[src]

#### `fn from(local_waker: LocalWaker) -> Self`[src]

Converts a `LocalWaker` into a `Waker`.

This conversion turns a `!Sync` `LocalWaker` into a `Sync` `Waker`, allowing a wakeup object to be sent to another thread, but giving up its ability to do specialized thread-local wakeup behavior.

### `impl From<bool> for AtomicBool`[src]

#### `fn from(b: bool) -> Self`[src]

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

### `impl From<bool> for i8`[src]

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

#### `fn from(small: bool) -> i8`[src]

### `impl From<bool> for i16`[src]

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

#### `fn from(small: bool) -> i16`[src]

### `impl From<bool> for i32`[src]

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

#### `fn from(small: bool) -> i32`[src]

### `impl From<bool> for i64`[src]

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

#### `fn from(small: bool) -> i64`[src]

### `impl From<bool> for i128`[src]

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

#### `fn from(small: bool) -> i128`[src]

### `impl From<bool> for isize`[src]

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

#### `fn from(small: bool) -> isize`[src]

### `impl From<bool> for u8`[src]

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

#### `fn from(small: bool) -> u8`[src]

### `impl From<bool> for u16`[src]

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

#### `fn from(small: bool) -> u16`[src]

### `impl From<bool> for u32`[src]

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

#### `fn from(small: bool) -> u32`[src]

### `impl From<bool> for u64`[src]

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

#### `fn from(small: bool) -> u64`[src]

### `impl From<bool> for u128`[src]

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

#### `fn from(small: bool) -> u128`[src]

### `impl From<bool> for usize`[src]

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

#### `fn from(small: bool) -> usize`[src]

### `impl From<char> for u32`[src]

#### `fn from(c: char) -> Self`[src]

Converts a `char` into a `u32`.

# Examples

    use std::mem;

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

### `impl From<f32> for f64`[src]

Converts `f32` to `f64` losslessly.

#### `fn from(small: f32) -> f64`[src]

### `impl From<i8> for isize`[src]

Converts `i8` to `isize` losslessly.

#### `fn from(small: i8) -> isize`[src]

### `impl From<i16> for isize`[src]

Converts `i16` to `isize` losslessly.

#### `fn from(small: i16) -> isize`[src]

### `impl From<i16> for AtomicI16`[src]

#### `fn from(v: i16) -> Self`[src]

Converts an `i16` into an `AtomicI16`.

### `impl From<i16> for f32`[src]

Converts `i16` to `f32` losslessly.

#### `fn from(small: i16) -> f32`[src]

### `impl From<i16> for f64`[src]

Converts `i16` to `f64` losslessly.

#### `fn from(small: i16) -> f64`[src]

### `impl From<i16> for i32`[src]

Converts `i16` to `i32` losslessly.

#### `fn from(small: i16) -> i32`[src]

### `impl From<i16> for i64`[src]

Converts `i16` to `i64` losslessly.

#### `fn from(small: i16) -> i64`[src]

### `impl From<i16> for i128`[src]

Converts `i16` to `i128` losslessly.

#### `fn from(small: i16) -> i128`[src]

### `impl From<i32> for AtomicI32`[src]

#### `fn from(v: i32) -> Self`[src]

Converts an `i32` into an `AtomicI32`.

### `impl From<i32> for f64`[src]

Converts `i32` to `f64` losslessly.

#### `fn from(small: i32) -> f64`[src]

### `impl From<i32> for i64`[src]

Converts `i32` to `i64` losslessly.

#### `fn from(small: i32) -> i64`[src]

### `impl From<i32> for i128`[src]

Converts `i32` to `i128` losslessly.

#### `fn from(small: i32) -> i128`[src]

### `impl From<i64> for AtomicI64`[src]

#### `fn from(v: i64) -> Self`[src]

Converts an `i64` into an `AtomicI64`.

### `impl From<i64> for i128`[src]

Converts `i64` to `i128` losslessly.

#### `fn from(small: i64) -> i128`[src]

### `impl From<i8> for AtomicI8`[src]

#### `fn from(v: i8) -> Self`[src]

Converts an `i8` into an `AtomicI8`.

### `impl From<i8> for f32`[src]

Converts `i8` to `f32` losslessly.

#### `fn from(small: i8) -> f32`[src]

### `impl From<i8> for f64`[src]

Converts `i8` to `f64` losslessly.

#### `fn from(small: i8) -> f64`[src]

### `impl From<i8> for i16`[src]

Converts `i8` to `i16` losslessly.

#### `fn from(small: i8) -> i16`[src]

### `impl From<i8> for i32`[src]

Converts `i8` to `i32` losslessly.

#### `fn from(small: i8) -> i32`[src]

### `impl From<i8> for i64`[src]

Converts `i8` to `i64` losslessly.

#### `fn from(small: i8) -> i64`[src]

### `impl From<i8> for i128`[src]

Converts `i8` to `i128` losslessly.

#### `fn from(small: i8) -> i128`[src]

### `impl From<isize> for AtomicIsize`[src]

#### `fn from(v: isize) -> Self`[src]

Converts an `isize` into an `AtomicIsize`.

### `impl From<u8> for usize`[src]

Converts `u8` to `usize` losslessly.

#### `fn from(small: u8) -> usize`[src]

### `impl From<u8> for isize`[src]

Converts `u8` to `isize` losslessly.

#### `fn from(small: u8) -> isize`[src]

### `impl From<u8> for char`[src]

Maps a byte in 0x00...0xFF to a `char` whose code point has the same value, in U+0000 to 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.

#### `fn from(i: u8) -> Self`[src]

Converts a `u8` into a `char`.

# Examples

    use std::mem;

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

### `impl From<u16> for usize`[src]

Converts `u16` to `usize` losslessly.

#### `fn from(small: u16) -> usize`[src]

### `impl From<u16> for AtomicU16`[src]

#### `fn from(v: u16) -> Self`[src]

Converts an `u16` into an `AtomicU16`.

### `impl From<u16> for f32`[src]

Converts `u16` to `f32` losslessly.

#### `fn from(small: u16) -> f32`[src]

### `impl From<u16> for f64`[src]

Converts `u16` to `f64` losslessly.

#### `fn from(small: u16) -> f64`[src]

### `impl From<u16> for i32`[src]

Converts `u16` to `i32` losslessly.

#### `fn from(small: u16) -> i32`[src]

### `impl From<u16> for i64`[src]

Converts `u16` to `i64` losslessly.

#### `fn from(small: u16) -> i64`[src]

### `impl From<u16> for i128`[src]

Converts `u16` to `i128` losslessly.

#### `fn from(small: u16) -> i128`[src]

### `impl From<u16> for u32`[src]

Converts `u16` to `u32` losslessly.

#### `fn from(small: u16) -> u32`[src]

### `impl From<u16> for u64`[src]

Converts `u16` to `u64` losslessly.

#### `fn from(small: u16) -> u64`[src]

### `impl From<u16> for u128`[src]

Converts `u16` to `u128` losslessly.

#### `fn from(small: u16) -> u128`[src]

### `impl From<u32> for AtomicU32`[src]

#### `fn from(v: u32) -> Self`[src]

Converts an `u32` into an `AtomicU32`.

### `impl From<u32> for f64`[src]

Converts `u32` to `f64` losslessly.

#### `fn from(small: u32) -> f64`[src]

### `impl From<u32> for i64`[src]

Converts `u32` to `i64` losslessly.

#### `fn from(small: u32) -> i64`[src]

### `impl From<u32> for i128`[src]

Converts `u32` to `i128` losslessly.

#### `fn from(small: u32) -> i128`[src]

### `impl From<u32> for u64`[src]

Converts `u32` to `u64` losslessly.

#### `fn from(small: u32) -> u64`[src]

### `impl From<u32> for u128`[src]

Converts `u32` to `u128` losslessly.

#### `fn from(small: u32) -> u128`[src]

### `impl From<u64> for AtomicU64`[src]

#### `fn from(v: u64) -> Self`[src]

Converts an `u64` into an `AtomicU64`.

### `impl From<u64> for i128`[src]

Converts `u64` to `i128` losslessly.

#### `fn from(small: u64) -> i128`[src]

### `impl From<u64> for u128`[src]

Converts `u64` to `u128` losslessly.

#### `fn from(small: u64) -> u128`[src]

### `impl From<u8> for AtomicU8`[src]

#### `fn from(v: u8) -> Self`[src]

Converts an `u8` into an `AtomicU8`.

### `impl From<u8> for f32`[src]

Converts `u8` to `f32` losslessly.

#### `fn from(small: u8) -> f32`[src]

### `impl From<u8> for f64`[src]

Converts `u8` to `f64` losslessly.

#### `fn from(small: u8) -> f64`[src]

### `impl From<u8> for i16`[src]

Converts `u8` to `i16` losslessly.

#### `fn from(small: u8) -> i16`[src]

### `impl From<u8> for i32`[src]

Converts `u8` to `i32` losslessly.

#### `fn from(small: u8) -> i32`[src]

### `impl From<u8> for i64`[src]

Converts `u8` to `i64` losslessly.

#### `fn from(small: u8) -> i64`[src]

### `impl From<u8> for i128`[src]

Converts `u8` to `i128` losslessly.

#### `fn from(small: u8) -> i128`[src]

### `impl From<u8> for u16`[src]

Converts `u8` to `u16` losslessly.

#### `fn from(small: u8) -> u16`[src]

### `impl From<u8> for u32`[src]

Converts `u8` to `u32` losslessly.

#### `fn from(small: u8) -> u32`[src]

### `impl From<u8> for u64`[src]

Converts `u8` to `u64` losslessly.

#### `fn from(small: u8) -> u64`[src]

### `impl From<u8> for u128`[src]

Converts `u8` to `u128` losslessly.

#### `fn from(small: u8) -> u128`[src]

### `impl From<usize> for AtomicUsize`[src]

#### `fn from(v: usize) -> Self`[src]

Converts an `usize` into an `AtomicUsize`.

### `impl<'a, T> From<&'a Option<T>> for Option<&'a T>`[src]

#### `fn from(o: &'a Option<T>) -> Option<&'a T>`[src]

### `impl<'a, T> From<&'a mut Option<T>> for Option<&'a mut T>`[src]

#### `fn from(o: &'a mut Option<T>) -> Option<&'a mut T>`[src]

### `impl<'a, T: ?Sized> From<&'a T> for NonNull<T>`[src]

#### `fn from(reference: &'a T) -> Self`[src]

### `impl<'a, T: ?Sized> From<&'a mut T> for NonNull<T>`[src]

#### `fn from(reference: &'a mut T) -> Self`[src]

### `impl<T> From<*mut T> for AtomicPtr<T>`[src]

#### `fn from(p: *mut T) -> Self`[src]

### `impl<T> From<T> for Option<T>`[src]

#### `fn from(val: T) -> Option<T>`[src]

### `impl<T> From<T> for Poll<T>`[src]

#### `fn from(t: T) -> Poll<T>`[src]

### `impl<T> From<T> for Cell<T>`[src]

#### `fn from(t: T) -> Cell<T>`[src]

### `impl<T> From<T> for RefCell<T>`[src]

#### `fn from(t: T) -> RefCell<T>`[src]

### `impl<T> From<T> for UnsafeCell<T>`[src]

#### `fn from(t: T) -> UnsafeCell<T>`[src]

### `impl<T> From<T> for T`[src]

#### `fn from(t: T) -> T`[src]

Loading content...

# Help

## Keyboard Shortcuts

`?`
  Show this help dialog
`S`
  Focus the search field
`↑`
  Move up in search results
`↓`
  Move down in search results
`↹`
  Switch tab
`⏎`
  Go to active search result
`+`
  Expand all sections
`-`
  Collapse all sections

## Search Tricks

Prefix searches with a type followed by a colon (e.g., `fn:`) to restrict the search to a given type.

Accepted types are: `fn`, `mod`, `struct`, `enum`, `trait`, `type`, `macro`, and `const`.

Search functions by type signature (e.g., `vec -> usize` or `* -> vec`)

Search multiple things at once by splitting your query with comma (e.g., `str,u8` or `String,struct:Vec,test`)