Alcuni metodi tratto hanno implementazioni predefinite che possono essere sovrascritte da un implementatore. Come posso utilizzare l'implementazione predefinita per una struttura che sovrascrive il valore predefinito?Utilizzo di un metodo tratto predefinito
esempio:
trait SomeTrait {
fn get_num(self) -> uint;
fn add_to_num(self) -> uint {
self.get_num() + 1
}
}
struct SomeStruct;
impl SomeTrait for SomeStruct {
fn get_num(self) -> uint { 3 }
fn add_to_num(self) -> uint {
self.get_num() + 2
}
}
fn main() {
let the_struct = SomeStruct;
println!("{}", the_struct.add_to_num()): // how can I get this to print 4 instead of 5?
}