Characterization of Data-Types

Various resources classify Rust data-types in the following manner:

Scalar Types:

  • Integers,
  • Floating Point Numbers,
  • Characters
  • Booleans For example, 12,12.3,‘a’,true are scalar types

Compound Types:

  • Tuples
  • Arrays

I have also seen the following (a struct) indicated as a type, but I have never seen it classified. Some_Struct<String, U32> They call it a type. Is there another type classification beyond scalar and compound types? Compound-compound types?

The types in Rust are listed here.
I am not entirely sure where you're getting your naming scheme of types from.

These are called primitive types.

These types, in addition to slices, are called sequence types.

1 Like

I think OP is coming from e.g. this page in the book

Data Types - The Rust Programming Language

I wouldn’t pay too much attention to this classification. One one hand, it is explicitly not intended to classify all types in Rust.

We’ll look at two data type subsets: scalar and compound.

There are other types beyond these two groups.

On the other hand, this page’s main purpose is to introduce a handful of (builtin) types that Rust provides out of the box. The main purpose is to give some examples to the fact that Rust has a type system in the first place, as well as introducing the ideas behind a handful of important builtin types.

As far as I can tell, the categorization of the types being presented as “scalar types” and “compound types” is mostly just done to give some structure to this page of the book, to group together the examples in some way that makes sense, so it’s less of an intimidating fairly long uninstructed list of examples, and thus easier to keep in your head.

4 Likes

I agree with @steffahn -- don't worry about classifying the built-in or std types into these types of categories, or taking that section as a whole too literally. It's giving you an overview.

3 Likes

There is other categorizations of types in Rust, orthogonal to this one, and also more objective and more generally applicable. For example

  • you can distinguish built-in types from user-defined types. The latter category could be split between “user”-defined types in the standard library, and truly user-defined types. (And there’s a grey area with regards to how built-in certain standard-library types are or aren’t.)
  • you can distinguish between “plain old data-types” that are handled on the stack, can be copied around, etc… from types other that are more abstract and more complex. The latter category might contain types that are simply more abstract, and can’t be copied around anymore, and types that (do / typically do / can) hold data on the heap. Within the last category, there’s types that can use an arbitrary / practically unlimited amound of memory (on the heap) and those that can’t.
  • you can distinguish types that have generic parameters and those that don’t
  • you can distinguish groups of types based on what traits they implement. This characterizes things like iterator types, thread-safe types, serializable types, etc… (Feel free to read further in the Rust book to learn about traits.) There’s some overlap of this kind of characterization with previous ones, e.g. POD (plain-old-data-) types are often characterized by implementing the Copy trait.

The types presented on this page of the book are all built-in, the “scalar” types are POD types, whereas tuples an arrays can be POD types, depending on whether their contents are POD types. Tuple types and array types have generic parameters (the latter even has a const generic length parameter), the listed “scalar” types don’t. All of them are thread-safe or serializable (in the case of tuples and arrays, again, only conditionally, based on the contained types); none of them are iterator types, but arrays implement the IntoIterator trait so you could call them “iterable” if you like that word.

As you can see, with regards to some of these examples of categorizations, the types presented on that page in the book are not all that diverse, and there’s lots of other kinds of types to learn about.

4 Likes

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.