How do traits in Rust differ from typeclasses in Haskell?
Hi everyone! I've been writing in Rust for about six months, and recently started learning Haskell. I noticed that traits in Rust are very similar to typeclasses in Haskell, but I feel there are fundamental differences between them that I don't fully understand yet.
For example, in Rust I can write code like this:
```rust
trait Speak {
fn speak(&self) -> String;
}
struct Dog;
impl Speak for Dog {
fn speak(&self) -> String {
"Woof!".to_string()
}
}
fn make_speak(animal: T) {
println!("{}", animal.speak());
}
```
In Haskell, as far as I understand, the equivalent looks like this:
```haskell
class Speak a where
speak :: a -> String
data Dog = Dog
instance Speak Dog where
speak _ = "Woof!"
makeSpeak :: Speak a => a -> IO ()
makeSpeak animal = p
No answers yet.
Log in to leave an answer