IT博客汇
  • 首页
  • 精华
  • 技术
  • 设计
  • 资讯
  • 扯淡
  • 权利声明
  • 登录 注册

    Rust traits act as generic constraints

    金庆发表于 2021-11-18 04:51:00
    love 0

    Rust traits act as generic constraints

    (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.

     



    金庆 2021-11-18 12:51 发表评论


沪ICP备19023445号-2号
友情链接