def bark(dog_dict):
print("WOOF!" if dog_dict["weight_kg"] > 25 else "Woof")
rex = {"name": "Rex", "weight_kg": 35}
This code is actually better, but it would even be better with structs and traitshttps://gist.github.com/rust-play/ceb5a292a22e55e27d56a83253...
We separate the concept of a Dog, the concept of something that barks, and the actual implementation in this case
We also don't immediately print it, but just return a slice so you can print it later or pass it to another function
It's a bit longer than the class example, but conceptually cleaner because multiple structs can implement the trait, some of them without a weight
and of course I'm only going to print things in my main, the interface only returns a slice which can be passed to other functions