heartpunk
heartpunk

Reputation: 2275

What does :: mean in Rust?

What does the :: syntax in Rust, as seen here, mean:

fn chunk(n: uint, idx: uint) -> uint {
    let sh = uint::BITS - (SHIFT * (idx + 1));
    (n >> sh) & MASK
}

In languages like Haskell it means a type hint, but here the compiler already has an annotation of that values type, so it seems it's likely type casting.

Upvotes: 37

Views: 23803

Answers (2)

Shepmaster
Shepmaster

Reputation: 432089

Please review Appendix B: Operators and Symbols of The Rust Programming Language.


In this case, the double colon (::) is the path separator. Paths are comprised of crates, modules, and items.

The full path for your example item, updated for 1.0 is:

std::usize::BITS

Here, std is the crate, usize is a module, and BITS is the specific item — in this case a constant.

If you scroll up in your file, you'll see use core::usize. use adds the path to the set of items to look in. That's how you can get away with just saying usize::BITS. The core crate is an implementation detail of the façade that is the std crate, so you can just substitute std for core in normal code.


:: can also be used as a way to specify generic types when they cannot otherwise be inferred; this is called the turbofish.

See also:

Upvotes: 44

heartpunk
heartpunk

Reputation: 2275

Oops. I wasn't reading very clearly. In this case, it's just the normal way of referring to anything under a module. uint::BITS is a constant, it seems.

Upvotes: 3

Related Questions