Object Orientation
Python
class Cat:
def __init__(self, name):
self.name = name
def greet(self, other):
print("Meow {}, I'm {}".format(other, self.name))
# ...
grumy = Cat('Grumpy')
grumy.greet('Garfield') # Meow Garfield, I'm Grumpy
Rust
struct Cat { name: String } impl Cat { pub fn new<S>(name: S) -> Cat where S: Into<String> { Cat { name: name.into() } } pub fn greet<S: Into<String>>(&self, other:S) { println!("Meow {}, I'm {}", other.into(), self.name); } } fn main() { let grumpy = Cat::new("Grumpy"); grumpy.greet("Garfield"); // Meow Garfield, I'm Grumpy }
NOTE: In Rust, it is best to avoid
stringly types APIs
so in the above example it would be better if we dolet garfield = Cat::new("Garfield")
and then makegreet
to accept an instance ofCat
asother
argument. If you are interested watch this.