(Jin Qing's Column, Nov. 18, 2021)
Rust traits are different from interfaces of C++/Java/Go.
See: https://stevedonovan.github.io/rustifications/2018/09/08/common-rust-traits.html
Rust traits are mechanism for adding behavior to types.
Traits have 2 modes. One is interface as Java.
Another is generic constraint. Generic functions are defined over types that implemented specific traits.
The "complie-time duck typing" in C++ templates is avoided in Rust. Rust will reject a type with quack() method as a Duck type. We must pass a type which implements Duck trait. But in Go, a type with quack() method is sufficient to be used as a Duck interface.
Powered by: C++博客 Copyright © 金慶