I don't really know how to get my point across effectively, so I'm going to present you with examples.
Look at this:
trait SuperCoolTrait {
fn myFunction(&self);
}
// Implementing the type itself
impl MyType {
fn function() {
unimplemented!()
}
}
// Implementing trait for the type
// Duplicating the type's name name, lots of extra
// and unnecessary typing
impl SuperCoolTrait for MyType {
fn myFunction(&self) {
unimplemented!()
}
}
and then compare that to something like this
trait SuperCoolTrait {
// --snip--
}
impl MyType with SuperCoolTrait {
fn function() {
unimplemented!()
}
// SuperCoolTrait
fn myFunction(&self) {
unimplemented!()
}
}
This shorthand just looks much cleaner (in my personal opinion), duplicates names less (if you are renaming a struct, that is one less place(s?) to rename). An even shorter shorthand would be to put the struct's functions directly inside of it... and why Rust's designers didn't do that I don't know (and maybe you can help me find out ). I imagine it would look something like this:
// Instead of renaming twice, you only have to do it once! (For the struct's declarations)
// P.S: Me using struct does not mean that this is necessarily exclusive to structs.
struct MyType with SuperCoolTrait {
// Semicolon instead of comma?
field: Foo;
fn function() {
unimplemented!();
}
fn myFunction(&self) {
unimplemented!();
}
}
I want to know what you guys think of such a design and if you have anything to add to the conversation.