Last active
March 13, 2025 19:20
-
-
Save DrLarck/a68e0d2996771efe33e7c996826ccac4 to your computer and use it in GitHub Desktop.
Appeler la méthode par défaut du Trait lors de la surcharge d'une méthode provenant d'un Trait
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
trait SomeTrait { | |
fn super_method(&self) { | |
println!("Hello world!"); | |
} | |
} | |
struct SomeStruct; | |
impl SomeTrait for SomeStruct {} | |
impl SomeStruct { | |
fn super_method(&self) { | |
// on utilise la "Fully Qualified Syntax" pour appeler la | |
// super méthode. | |
// Appeler self.super_method() directement risquerait de | |
// faire overflow le buffer car ce serait un appel | |
// récursif. Pour enlever toute ambiguité on utilise | |
// la Fully Qualified Syntax, donc on spécifie le nom du | |
// Trait ainsi que la méthode a appeler. | |
// | |
// c.f | |
// https://doc.rust-lang.org/stable/book/ch20-03-advanced-traits.html#fully-qualified-syntax-for-disambiguation-calling-methods-with-the-same-name | |
SomeTrait::super_method(self); | |
// on ajoute le comportement supplémentaire lors de l'override | |
// de la méthode provenant de SomeTrait | |
println!("Some extra behavior"); | |
} | |
} | |
fn main() { | |
let some_struct = SomeStruct; | |
some_struct.super_method(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment