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

    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::io::{self, Read};
    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 file = std::fs::File::open("test")?;
        let mut contents = String::new();
        file.read_to_string(&mut contents)?;
        let num: i32 = contents.trim().parse()?;
        Ok(num)
    }Run

## Required Methods

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

Performs the conversion.

## Implementors

- `impl From<!> for TryFromIntError` | [src]

- `impl From<bool> for u8` | [src]

- `impl From<bool> for u16` | [src]

- `impl From<bool> for u32` | [src]

- `impl From<bool> for u64` | [src]

- `impl From<bool> for u128` | [src]

- `impl From<bool> for usize` | [src]

- `impl From<bool> for i8` | [src]

- `impl From<bool> for i16` | [src]

- `impl From<bool> for i32` | [src]

- `impl From<bool> for i64` | [src]

- `impl From<bool> for i128` | [src]

- `impl From<bool> for isize` | [src]

- `impl From<u8> for u16` | [src]

- `impl From<u8> for u32` | [src]

- `impl From<u8> for u64` | [src]

- `impl From<u8> for u128` | [src]

- `impl From<u8> for usize` | [src]

- `impl From<u16> for u32` | [src]

- `impl From<u16> for u64` | [src]

- `impl From<u16> for u128` | [src]

- `impl From<u32> for u64` | [src]

- `impl From<u32> for u128` | [src]

- `impl From<u64> for u128` | [src]

- `impl From<i8> for i16` | [src]

- `impl From<i8> for i32` | [src]

- `impl From<i8> for i64` | [src]

- `impl From<i8> for i128` | [src]

- `impl From<i8> for isize` | [src]

- `impl From<i16> for i32` | [src]

- `impl From<i16> for i64` | [src]

- `impl From<i16> for i128` | [src]

- `impl From<i32> for i64` | [src]

- `impl From<i32> for i128` | [src]

- `impl From<i64> for i128` | [src]

- `impl From<u8> for i16` | [src]

- `impl From<u8> for i32` | [src]

- `impl From<u8> for i64` | [src]

- `impl From<u8> for i128` | [src]

- `impl From<u16> for i32` | [src]

- `impl From<u16> for i64` | [src]

- `impl From<u16> for i128` | [src]

- `impl From<u32> for i64` | [src]

- `impl From<u32> for i128` | [src]

- `impl From<u64> for i128` | [src]

- `impl From<u16> for usize` | [src]

- `impl From<u8> for isize` | [src]

- `impl From<i16> for isize` | [src]

- `impl From<i8> for f32` | [src]

- `impl From<i8> for f64` | [src]

- `impl From<i16> for f32` | [src]

- `impl From<i16> for f64` | [src]

- `impl From<i32> for f64` | [src]

- `impl From<u8> for f32` | [src]

- `impl From<u8> for f64` | [src]

- `impl From<u16> for f32` | [src]

- `impl From<u16> for f64` | [src]

- `impl From<u32> for f64` | [src]

- `impl From<f32> for f64` | [src]

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

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

- `impl<T> From<T> for T` | [src]

- `impl From<bool> for AtomicBool` | [src]

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

- `impl From<i8> for AtomicI8` | [src]

- `impl From<u8> for AtomicU8` | [src]

- `impl From<i16> for AtomicI16` | [src]

- `impl From<u16> for AtomicU16` | [src]

- `impl From<i32> for AtomicI32` | [src]

- `impl From<u32> for AtomicU32` | [src]

- `impl From<i64> for AtomicI64` | [src]

- `impl From<u64> for AtomicU64` | [src]

- `impl From<isize> for AtomicIsize` | [src]

- `impl From<usize> for AtomicUsize` | [src]

- `impl<T> From<T> for Cell<T>` | [src]

- `impl<T> From<T> for RefCell<T>` | [src]

- `impl<T> From<T> for UnsafeCell<T>` | [src]

- `impl From<char> for u32` | [src]

- `impl From<u8> for char` | [src]

- `impl<T> From<T> for Option<T>` | [src]

- `impl<'a, T> From<FutureObj<'a, T>> for LocalFutureObj<'a, T>` | [src]

- `impl<T> From<T> for Poll<T>` | [src]

- `impl From<LocalWaker> for Waker` | [src]

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