On this page
  1. From
  2. Availability
  3. Syntax & Overview
  4. Notes & Pitfalls
  5. Example

From

Prev: Iterator | Up: builtins | Next: Send
Rel: builtins, Ownership, Borrowing & Lifetimes
Ref: Rust Docs — From
Tags: #public #prog #rust #builtins #tier/core


Availability

Built-in / Prelude (Prelude Trait) — available with zero use imports.

Syntax & Overview

trait From<T> { fn from(value: T) -> Self; }  /* + Into<U> */

Infallible value-to-value conversion; implementing From<A> for B automatically provides Into<B> for A and enables ? error conversion.

Notes & Pitfalls

Example

#[derive(Debug, PartialEq)]
struct UserId(u64);

impl From<u64> for UserId {
    fn from(id: u64) -> Self { Self(id) }
}

fn main() {
    let a = UserId::from(42);
    let b: UserId = 42u64.into();
    assert_eq!(a, b);
}

Linked from

Related notes