HI there, I'm new to the programming world and begin with rust. I've wondered what is a type as in '' bringing a type called std::cmp::Ordering
into scope".
What does a type mean in this context? What is the relationship betwen cmp and ordering?
In this case, Ordering
is the name of the type, cmp
is the name of the module containing the Ordering
type and std::cmp::Ordering
is the path to the type Ordering
.
4 Likes
Every variable, and more generally every value, has a type. In addition to the built in types, Rust programs and libraries can define their own types. You can use a few of these without declaring that you're going to use them, for example Option
and Vec
. But usually you have to bring it in to scope or type out the full path to the type.
More generally, from the book,
- Here is more on the basic data types in Rust
- The chapter on
struct
s, which are data types which can group many values - The chapter on
enum
s, which are data types which can take on one of an explicit list of variants
And std::cmp::Ordering
in particular is an enum
, and you can see its definition here. From that definition, every value that has the type Ordering
is equal to one of:
Ordering::Less
Ordering::Equal
Ordering::Greater
2 Likes