简体   繁体   中英

What does `impl … for` mean?

I see the following pattern coming up a lot in Rust codebases, but I can't find an explanation why this is used.

For what end does one use the impl... for construction?

Pseudocode:

impl Handler {
    pub fn method1() -> () {
    }
}
impl Dummy for Handler {
    pub fn method2() -> () {
    }
}

impl Struct... adds some methods to Struct . These methods aren't available to other types or traits.

impl Trait for Struct.. implements the trait Trait for the struct Struct . This results in the methods of the trait being available for Struct .

So, even though these two syntaxes look similar, they do 2 completely different things. impl Struct... adds new (not previously defined) methods to the type, while the other adds previously defined methods (from the trait) to the type.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM