Data Types
Get familiar with Rust's basic and compound data types.
Data Types
Rust is a strongly typed language, meaning that every value has a specific type. The compiler performs type checking to ensure code correctness.
Basic Types
Integers
RUSTlet x: i32 = 42; // 32-bit signed integer let y: u64 = 100; // 64-bit unsigned integer let z: isize = -10; // architecture-dependent size
Floating Point Numbers
RUSTlet x: f32 = 3.14; // 32-bit floating point let y: f64 = 2.718; // 64-bit floating point (default)
Boolean
RUSTlet is_rust_awesome: bool = true; let is_easy: bool = false;
Characters
RUSTlet c: char = 'z'; // Unicode character let emoji: char = '🦀';
Compound Types
Tuple
RUSTlet tup: (i32, f64, bool) = (500, 6.4, true); let (x, y, z) = tup; // Destructuring
Array
RUSTlet arr: [i32; 5] = [1, 2, 3, 4, 5]; let first = arr[0];
Type Inference
Rust can often infer the type:
RUSTlet x = 42; // i32 (default) let y = 3.14; // f64 (default)
Blockchain Data Types
In blockchain development, we commonly use:
- u64/u128: For token amounts and balances (no negative values)
- String: For addresses (e.g., "0x742d35Cc6634C0532925a3b844Bc9e7595f0bEb")
- [u8; 32]: For 256-bit hashes (32 bytes)
- bool: For validation flags and status
RUSTlet balance: u64 = 1_000_000_000; // Token balance (wei, satoshi, etc.) let address: String = String::from("0x..."); // Blockchain address let block_hash: [u8; 32] = [0; 32]; // 256-bit hash let is_valid: bool = true; // Transaction validity
Code Examples
Integers
Using different integer types
fn main() {
let small: i8 = -128;
let medium: i32 = 1_000_000;
let large: i64 = 9_223_372_036_854_775_807;
println!("Small: {}", small);
println!("Medium: {}", medium);
println!("Large: {}", large);
}Explanation:
Rust offers different sized integer types. `i8` is 8-bit, `i32` is 32-bit, and `i64` is 64-bit signed integer.
Using Tuples
Creating and using tuples
fn main() {
let person: (String, i32, bool) = (
String::from("John"),
25,
true
);
println!("Name: {}", person.0);
println!("Age: {}", person.1);
println!("Active: {}", person.2);
let (name, age, active) = person;
println!("{} years old, active: {}", age, active);
}Explanation:
A tuple allows us to package different types of values together. We can access elements by index (0, 1, 2...) or by destructuring.
Blockchain Data Types
Common data types in blockchain development
fn main() {
let balance: u64 = 1_000_000_000;
let fee: u64 = 21_000;
let sender: String = String::from("0x742d35Cc6634C0532925a3b844Bc9e7595f0bEb");
let receiver: String = String::from("0x8ba1f109551bD432803012645Hac136c22C9e");
let block_height: u64 = 18_500_000;
let block_number: u64 = 18_500_001;
let is_valid: bool = true;
let is_confirmed: bool = false;
println!("Balance: {} tokens", balance);
println!("From: {}", sender);
println!("To: {}", receiver);
println!("Block height: {}", block_height);
println!("Valid: {}, Confirmed: {}", is_valid, is_confirmed);
}Explanation:
Blockchain applications use specific data types: u64/u128 for amounts (no negatives), String for addresses, and bool for validation. This ensures type safety and prevents errors.
Exercises
Practicing Types
Create variables of different types and print their values!
Starter Code:
fn main() {
}