by 2h · 3y ago · 0 comments
package hello
type namer interface {
first() string
last() string
}
func name(n namer) string {
return n.first() + " " + n.last()
}
I think I saw on a blog post that Rust can only do this with some hacks, is that true? trait Namer {
fn first(&self) -> String;
fn last(&self) -> String;
// You can put this here if you want `namer.name()`
fn name(&self) -> String {
format!("{} {}", self.first(), self.last())
}
}
fn name(n: &impl Namer) -> String {
format!("{} {}", n.first(), n.last())
}