3 ms·
question for all you Rust people. does Rust have a way to use interfaces like Go? For example with Go I can do this: package hello type namer inte
by 2h 3y ago
question for all you Rust people. does Rust have a way to use interfaces like Go? For example with Go I can do this:
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?
- kaikalii 3y agotrait 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()) }
- 2h 3y agocool much thanks, one more reason to move to Rust hehe - since "let else" was added its looking pretty attractive
- iknowstuff 3y agolet else is a rather minor addition, surprising that that's what's winning you over