Module show_notes::e025[][src]

Traits Deep Dive, Part 3

Show Notes

Sponsored by Parity Technologies! Parity is hiring Rust developers so if you're interested, you should check out their job listings!

Example

You can see all of the pieces of the final example described in the show here (and the module has the required definitions for Point).

let points = vec![
    Point { x: 1.0, y: 2.0 },
    Point { x: 12.0, y: 4.3 },
    Point { x: -5.4, y: 18.7 },
];

let origin = Point::default();

// This is the version we start with. It works fine, but it's not elegant.
let distances_inline: Vec<f32> = points
    .iter()
    .map(|point| {
        let change = point - &origin;
        (change.x.powi(2) + change.y.powi(2)).sqrt()
    })
    .collect();

// This version is *much* cleaner!
let distances_impl: Vec<f32> = points.iter().map(distance_from_impl(&origin)).collect();

Sponsors

(Thanks to the couple people donating who opted out of the reward tier, as well. You know who you are!)

Become a sponsor

Contact

Structs

Point
Script

e025: Traits Deep Dive, Part III

Functions

distance_from_alias
distance_from_boxed
distance_from_impl

Type Definitions

DistanceFrom