Rust calling order with the same method name

Sample code:

fn main() {
    (T).say();
}

Rule one:

1. Call say(self)

2. Call say(&self)

3. Call say(&mut self). If it exists but self is immutable, direct compilation will fail.

4. Dereference. If dereference fails, compilation fails directly.

5. Call say(self). If it exists but self does not implement copy, compilation will fail directly.

6. Call say(&self)

7. Call say(&mut self). If the parent type is not immutable, DerefMut is not implemented, or &mut cannot be obtained, the compilation will fail directly.

8. Repeat 4

Rule two:

When a trait implementation has the same name as its own implementation, the own implementation will always be called first.

Guess you like

Origin blog.csdn.net/u013259665/article/details/122184231