For example, I have two structs HumanPlayer and ComputerPlayer. Both of them have similar fields:
name: String, score: u32, bonus_card: u32,
They also have similar functionality except for the function
fn pick_card(&self), for the
HumanPlayer it asks the user if he/she wants to pick a card. For the
ComputerPlayer the decision is done using another helper function which uses some logic to decide if it needs extra card or not. Let's call this extra function
fn pick_or_pass(&self, human_score: HumanPlayer) -> bool, which is only available for a
With inheritance it's straightforward. I'll use the abstract class Player and both, Human and Computer, will inherit from it, and I'll just add an extra function for Computer and override the other one.
However, what is the optimal solution to this in Rust? just write similar boilerplate code for both structs separately? and create a trait, which will have general implementation of the shared functions?
Or define a field
ComputerPlayer? and then if I want to call something from
main I'll call something like
Any suggestions - best practices?