datatypes in rust
Rust is a statically typed language, which means every value has a data type that the compiler
must know at compile time [1] [2] [3] . The compiler can often infer the type based on the value
and its usage, but sometimes a type annotation is required [1] [4] .
Rust's data types are categorized into scalar, compound, and custom types [5] [6] [3] .
Scalar Types
Scalar types represent a single value [1] [6] . Rust has four primary scalar types [1] [4] [2] .
Integers These are whole numbers without a fractional component [6] . They come in signed
(i) and unsigned (u) variants and in sizes of 8, 16, 32, 64, and 128 bits, as well as isize and
usize which depend on the computer's architecture [7] . The default integer type is i32 [4] .
Floating-Point Numbers These are numbers with decimal points [8] . Rust offers two types:
f32 (single-precision) and f64 (double-precision) [5] . The default type is f64 [7] .
Booleans The boolean type, bool, has two possible values: true or false [7] [4] .
Characters The char type represents a single Unicode character and is specified with single
quotes [7] [8] . It is four bytes in size [2] .
Compound Types
Compound types can group multiple values into one type [2] [3] .
Tuples A tuple is a way of grouping together a number of values with a variety of types into
one compound type. Tuples have a fixed length and cannot grow or shrink in size once
declared [3] .
Arrays An array allows you to group a collection of values of the same type [6] . Like tuples,
arrays in Rust have a fixed length [6] . For a dynamic or growable collection, Rust's standard
library provides the vector (Vec) type [6] .
Custom and Other Types
Rust also allows for the creation of custom data types and has specific types for handling text [6]
[9] .
Structs Structures, or structs, are custom data types that let you name and package
together multiple related values that make up a meaningful group [6] . You can group
different data types into a single composite type [6] .
Enums Enumerations, or enums, allow you to define a type by enumerating its possible
variants [5] . They are useful for creating a type that can be one of several different
possibilities, such as the Option and Result enums for handling optional values and errors [5] .
Strings Rust has two main string types: &str (a "string slice") and String [6] . A string slice is
a reference to a sequence of characters, is immutable, and has a fixed size [7] [6] . The
String type is a growable, mutable, owned, UTF-8 encoded string type provided by Rust's
standard library [6] .
⁂
1. https://doc.rust-lang.org/book/ch03-02-data-types.html
2. https://web.mit.edu/rust-lang_v1.25/arch/amd64_ubuntu1404/share/doc/rust/html/book/second-edition/
ch03-02-data-types.html
3. https://www.youtube.com/watch?v=NyqJp5M3hRE
4. https://www.programiz.com/rust/data-types
5. https://dev.to/francescoxx/rust-data-types-1mlg
6. https://zerotomastery.io/blog/rust-data-types/
7. https://www.w3schools.com/rust/rust_data_types.php
8. https://www.tutorialspoint.com/rust/rust_data_types.htm
9. https://doc.rust-lang.org/reference/types.html