Data Types in Rust
Rust is a statically typed language, which means the compiler must know the types of all variables at compile time. Let’s explore the fundamental data types in Rust.String Types
Rust has two main string types, each serving different purposes:Key Difference:
&str is a reference to string data (borrowed), while String owns the string data and can be modified. &str is stored on the stack, String is stored on the heap.Integer Types
Rust provides both signed and unsigned integers in various sizes:Signed Integers (i8, i16, i32, i64, i128)
Unsigned Integers (u8, u16, u32, u64, u128)
Integer Type Ranges
Integer Type Ranges
| Type | Signed Range | Unsigned Range |
|---|---|---|
| 8-bit | -128 to 127 | 0 to 255 |
| 16-bit | -32,768 to 32,767 | 0 to 65,535 |
| 32-bit | -2,147,483,648 to 2,147,483,647 | 0 to 4,294,967,295 |
| 64-bit | -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807 | 0 to 18,446,744,073,709,551,615 |
Floating-Point Types
Rust has two floating-point types for decimal numbers:f64 is the default floating-point type because on modern CPUs, it’s roughly the same speed as f32 but with more precision.Character Type
Thechar type represents a single Unicode scalar value:
Compound Types
Tuples
Tuples group multiple values of different types into one compound type:Arrays
Arrays are fixed-size collections of elements of the same type:Array syntax:
[type; size] where type is the element type and size is the number of elements.The syntax [value; size] creates an array with size elements, all set to value.Type Conversion and Parsing
Converting between types is a common operation in Rust:Type Inference
Rust is smart about inferring types:Copy vs Move Types
Understanding which types areCopy is crucial for working with ownership:
Copy Types (Copied, not moved)
Copy Types (Copied, not moved)
These types are stored entirely on the stack and implement the
Copy trait:- All integer types:
i8,i16,i32,i64,i128,u8,u16,u32,u64,u128 - Floating-point types:
f32,f64 - Boolean:
bool - Character:
char - Tuples containing only
Copytypes:(i32, i32)
Move Types (Ownership transferred)
Move Types (Ownership transferred)
These types are stored on the heap and do NOT implement
Copy:StringVec<T>- Any type that owns heap data
- Tuples containing any non-
Copytype
Quick Reference Table
| Type | Example | Copy? | Size | Use Case |
|---|---|---|---|---|
&str | "hello" | Yes | 2 words | String literals, borrowed strings |
String | String::from("hi") | No | 3 words + heap | Owned, mutable strings |
i32 | 42 | Yes | 4 bytes | General-purpose integers |
u32 | 42 | Yes | 4 bytes | Non-negative integers |
f64 | 3.14 | Yes | 8 bytes | Floating-point numbers |
char | 'A' | Yes | 4 bytes | Unicode characters |
bool | true | Yes | 1 byte | Boolean values |
[i32; 5] | [1,2,3,4,5] | Yes* | 20 bytes | Fixed-size arrays |
(i32, String) | (1, String::from("hi")) | No | Varies | Mixed-type tuples |
Copy only if their element type is Copy
Key Takeaways
Next Steps
- Ownership - Learn how ownership works with different types
- Rock Paper Scissors - Apply data types in a real project