Module show_notes::e025

source ·
Expand description

Traits Deep Dive, Part 3

  • Date: July 4, 2018
  • Subject: Closure traits, impl trait, dyn trait, and object safety!
  • Script
  • Audio

§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§

Functions§

Type Aliases§