Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does :: mean in Rust?

Tags:

syntax

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.

like image 404
heartpunk Avatar asked Oct 26 '15 16:10

heartpunk


People also ask

What is the meaning of the&symbol in rust?

There are two things here. First is the `&` symbol which has similar meaning : (a reference to a variable) as in C/C++. The next symbol ` ' ` is actually not attached to the & symbol, but it is seen before a type followed by a single lower case letter in rust like ( &'a u32 ) or ( &'m Vec<f64>).

How do you call a method in rust?

Rust has a feature called automatic referencing and dereferencing. Calling methods is one of the few places in Rust that has this behavior. Here’s how it works: when you call a method with object.something (), Rust automatically adds in &, &mut, or * so object matches the signature of the method.

What is rust?

What is rust? In steel component which is an alloy of Fe and minor elements for the purpose they serve. In service or actual use or while transporting, these steels may get corroded means Fe may get oxidized by ambient oxygen or moisture by electro-chemical reactions.

What is the difference between&and'first in rust?

First is the `&` symbol which has similar meaning : (a reference to a variable) as in C/C++. The next symbol ` ' ` is actually not attached to the & symbol, but it is seen before a type followed by a single lower case letter in rust like ( &'a u32 ) or ( &'m Vec<f64>).


2 Answers

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:

  • What is the syntax: `instance.method::<SomeThing>()`?
like image 156
Shepmaster Avatar answered Sep 20 '22 15:09

Shepmaster


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.

like image 24
heartpunk Avatar answered Sep 23 '22 15:09

heartpunk