Hi all, apologies for the noob question, but I'm working my way through the rustlings course and I'm stuck at the traits3.rs question, where we are expected to provide a default implementation of a trait to allow the the tests to pass.
For reference, the question is:
/ traits3.rs
//
// Your task is to implement the Licensed trait for
// both structures and have them return the same
// information without writing the same function twice.
//
// Consider what you can add to the Licensed trait.
// Execute `rustlings hint traits3` or use the `hint` watch subcommand for a hint.
// I AM NOT DONE
pub trait Licensed {
fn licensing_info(&self) -> String
}
struct SomeSoftware {
version_number: i32,
}
struct OtherSoftware {
version_number: String,
}
impl Licensed for SomeSoftware {} // Don't edit this line
impl Licensed for OtherSoftware {} // Don't edit this line
My naive fix was to provide a default implementation that accessed the version_number, e.g.:
If the goal is to provide a default information, and your trait has no other methods or super-traits which you can use to find out more about self (e.g. getters), your only real choice is to return a hard-coded value.