Rust 中的 ::
语法是什么,如 here 所示, 意思是:
fn chunk(n: uint, idx: uint) -> uint {
let sh = uint::BITS - (SHIFT * (idx + 1));
(n >> sh) & MASK
}
在像 Haskell 这样的语言中,它意味着类型提示,但在这里编译器已经有了该值类型的注释,所以它看起来很可能是类型转换。
最佳答案
请查看Appendix B: Operators and Symbols Rust 编程语言。
在这种情况下,双冒号 (::
) 是路径分隔符。 Paths由 crates、modules 和 items 组成。
your example item, updated for 1.0 的完整路径是:
std::usize::BITS
这里,std
是包,usize
是一个模块,BITS
是具体的项目——在本例中是一个常量。
如果向上滚动文件,您会看到use core::usize
。 use
将路径添加到要查看的项目集。这就是您只需说 usize::BITS
就可以摆脱困境的方法。 core
crate 是一个 implementation detail of the façade that is the std
crate , 所以你可以在普通代码中用 std
替换 core
。
::
也可以用作在无法以其他方式推断泛型类型时指定泛型类型的方法;这叫做 turbofish .
另见:
关于syntax -::在 Rust 中是什么意思?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33350721/