Flexible trait function parameter names in Rust

coolshaurya

Shaurya

Posted on April 12, 2020

Flexible trait function parameter names in Rust

When we define a trait we give it a function statement of the form fn blah(foo: Bar). But when implementing this trait the parameter name (here it is foo) can be changed as you wish. So let's have an example.

Playground

/// A very simple struct
struct Simple;
/// A struct that doesn't matter
struct Whatever;

/// A trait that does something _very_ complex
trait SuperDuperComplexThing {
    fn do_super_duper_complex_thing(some_extremely_and_needlessly_long_parameter: Whatever) -> Whatever ;
}

impl SuperDuperComplexThing for Simple {
    fn do_super_duper_complex_thing(simple_param: Whatever) -> Whatever {
        Whatever
    }
}
Enter fullscreen mode Exit fullscreen mode

And this works perfectly. It compiles.

💖 💪 🙅 🚩
coolshaurya
Shaurya

Posted on April 12, 2020

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related