Home
Rust
From Example (impl)
Updated Dec 27, 2023
Dot Net Perls
From. In Rust programs, we often need to convert between types with the from() and into() functions. To enable these functions for custom structs, we can implement the "From" trait.
With the impl keyword, we can provide an implementation for the From trait. In the from function, we receive the source type, and return Self.
Example. This program contains 2 custom structs, Range and RangeCompressed. In Range, we have 2 usize fields, and in RangeCompressed, these fields have been changed to u16 fields.
struct
Here We provide an impl From that specifies Range and RangeCompressed as its source and target types.
And The from() function within the From impl block receives a Range, and returns a RangeCompressed (with its fields cast).
Version 1 We create a Range, and then call into() to get a RangeCompressed. We verify with println that the start and end fields are correct.
Version 2 With the From trait, we get both into() and from() functions. Here we call from() on the RangeCompressed type.
struct Range { start: usize, end: usize, } struct RangeCompressed { start: u16, end: u16, } impl From<Range> for RangeCompressed { fn from(range: Range) -> Self { // For from, just cast the internal fields of the struct. RangeCompressed { start: range.start as u16, end: range.end as u16, } } } fn main() { // Version 1: use into to convert one struct to another with From trait. let range = Range { start: 0, end: 10 }; let range_compressed: RangeCompressed = range.into(); println!( "start: {} end: {}", range_compressed.start, range_compressed.end ); // Version 2: use from method for conversion. let range2 = Range { start: 50, end: 100, }; let range_compressed2 = RangeCompressed::from(range2); println!( "start: {} end: {}", range_compressed2.start, range_compressed2.end ); }
start: 0 end: 10 start: 50 end: 100
Some notes. Rust programs typically contain a significant amount of code that casts and converts types. This is required as the language is strict with type-checking.
usize
And With the From trait, we can simplify some of these conversions. This can reduce code duplication with zero performance cost.
Important For some optimizations, like reducing the byte size of a struct, the From trait can be useful and elegant.
The From trait is not necessary in Rust code, but having it present can simplify how programs are written. It can make programs more elegant. And this is often worth the effort involved.
Dot Net Perls is a collection of pages with code examples, which are updated to stay current. Programming is an art, and it can be learned from examples.
Donate to this site to help offset the costs of running the server. Sites like this will cease to exist if there is no financial support for them.
Sam Allen is passionate about computer languages, and he maintains 100% of the material available on this website. He hopes it makes the world a nicer place.
This page was last updated on Dec 27, 2023 (new).
Home
Changes
© 2007-2025 Sam Allen