liveBook · Manning

From trait

From is Rust’s standard-library trait for infallibly converting a value of one type into another type.

How 4 Manning books explain it

Rust Servers, Services, and Apps

From is the Rust standard-library trait for defining infallible conversions from one type into another.

How it is used
Converts HTTP request data, response structures, wrapped JSON inputs, and distinct errors across server-side type boundaries.
How deep it goes
Works through concrete From implementations and unit tests, then outlines error conversion without showing its implementation.
Examples
Tests GET and HTTP/1.1 conversions, serializes a 404 response, and unwraps an Actix Web JSON input model.
What you can do after
Build it from scratch for request parsing and response serialization; apply it in Actix Web model and error workflows.

Read how Rust Servers, Services, and Apps explains From trait →

Rust in Action: Systems programming concepts and techniques

From is a Rust standard-library trait for infallibly converting a value of one type into another type.

How it is used
Uses From to support value conversions and to bring multiple upstream error types into one wrapper error type.
How deep it goes
Works through bidirectional numeric implementations and separate error-wrapper implementations, including how the latter remove repeated map_err calls.
Examples
Shows Q7 conversions with f64 and f32, wrapper conversions for I/O and address-parsing errors, and NTP timestamp conversions.
What you can do after
Build it from scratch by implementing From for numeric types or wrapper errors, then use from(), into(), or ? where appropriate.

Read how Rust in Action: Systems programming concepts and techniques explains From trait →

Code Like a Pro in Rust

From is a Rust standard-library trait for infallible conversion from one type into another type.

How it is used
Standardizes conversions between types, especially converting I/O or SQLx errors into an application’s Result error type.
How deep it goes
Defines the trait signature and works through wrapper and custom-error implementations, including a missing-implementation compiler diagnostic.
Examples
Shows a string wrapper, file-reading errors, and SQLx queries using `.map_err(Into::into)`.
What you can do after
Build it from scratch by implementing From for a wrapper or custom error, then use it with `?` or `map_err`.

Read how Code Like a Pro in Rust explains From trait →

Learn Rust in a Month of Lunches

The From trait defines conversion of one Rust type into another specified type.

How it is used
Uses explicit conversions to connect standard-library types and give custom types convenient construction syntax.
How deep it goes
Explains the conversion rule, standard-library examples, custom implementations, blanket relationships, and the orphan-rule boundary.
Examples
Demonstrates array, string, byte-vector, City, Country, BTreeMap, and error conversions.
What you can do after
Build it from scratch by implementing conversions for custom types and invoking them with From::from or Into.

Read how Learn Rust in a Month of Lunches explains From trait →

Related concepts

These guides are generated from the text of Manning books by Manning's knowledge graph and link to the sections they draw on. The full books are available on liveBook and at manning.com.