rust - 'Zero Cost Abstraction' 是什么意思?

标签 rust

我在探索 Rust 时遇到了这个术语。
我看到了关于这个的不同类型的解释,但仍然不太明白。
在嵌入式 Rust 书中,它说

Type states are also an excellent example of Zero Cost Abstractions

  • the ability to move certain behaviors to compile time execution or analysis.

These type states contain no actual data, and are instead used as markers.

Since they contain no data, they have no actual representation in memory at runtime:


这是否意味着运行时更快,因为运行时没有内存?
如果有人能以易于理解的方式解释它,请欣赏它。

最佳答案

零成本抽象意味着添加更高级别的编程概念,如泛型、集合等不会带来运行时成本,只有编译器时间成本(代码编译会更慢)。任何对零成本抽象的操作都与您使用低级编程概念(for 循环、计数器、ifs 等)手动编写匹配功能一样快。

关于rust - 'Zero Cost Abstraction' 是什么意思?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69178380/

相关文章:

string - 如何编写一个同时接受拥有和非拥有的字符串集合的函数?

rust - 如何将无效的JSON请求正文中的错误描述返回给Rust中的客户端?

operator-overloading - 如何在Rust中实现惯用运算符重载值和引用?

rust - 如何将 number::Rational 转换为十进制数?

rust - 为什么我会收到错误 E0277 : the size for values of type `[{integer}]` cannot be known at compilation time?

rust - 如何通过 FFI 公开编译时生成的静态 C 字符串?

types - 什么时候仅在运行时才知道类型?

types - 在类型别名上调用静态方法

process - 我如何等待标准输出已通过管道传输到另一个的 Rust `Child` 进程?

generics - 通用函数和类型如何存储在 rlib 中?