Methods being magically attached to tuples?

I've been reading through the documentation of the ndarray crate, and found some pattern I didn't know before. In Javascript land, this would be impossible. The method f() does not exist on tuples.

Is this some sort of sorcery or can you do this to any type in Rust? Importing some crate would instantly make methods available on traits that didn't have it before?

How is this called and when is it used?

use ndarray::{ShapeBuilder};

fn main(){

   let x = (2, 2, 2, 2).f();

   dbg!(x);
}

It's just implementing a local trait on a foreign type.

impl<T> ShapeBuilder for T where
    T: IntoDimension, 
impl IntoDimension for (Ix, Ix, Ix, Ix)

trait Trait {
    fn f(&self) {}
}

impl Trait for (u32, i32, f32, f64) {}

fn main() {
    (0, 0, 0.0, 0.0).f();
}

well this is cool. Is it used a lot by programmers?

Are traits used a lot in general? Yes, all the time.

Are local traits used to extend foreign types a lot? It's pretty common, particularly when the trait makes sense for std types and/or for blanket implementations based on std traits.

Are local traits used to extend tuples specifically a lot? Seems more niche to me, other than a blanket implementation happening to apply to tuples. But there are use cases, like the ndarray one or lazily and (relatively) ergonomically constructing errors for ?.

1 Like