Integer types let you store whole numbers in your program. They help you work with counts, ages, or any number without fractions.
Integer types in Rust
let variable_name: integer_type = value;
Integer types in Rust include signed (can be negative) and unsigned (only positive) numbers.
Common types: i8, i16, i32, i64, i128 (signed) and u8, u16, u32, u64, u128 (unsigned).
age with value 30.let age: i32 = 30;count with the max value 255.let count: u8 = 255;temperature with a negative value.let temperature: i16 = -10;This program stores counts of fruits using unsigned integers and a temperature using a signed integer. It then prints the results.
fn main() {
let apples: u8 = 10;
let oranges: u8 = 20;
let total_fruits: u8 = apples + oranges;
println!("Total fruits: {}", total_fruits);
let temperature: i32 = -5;
println!("Temperature: {} degrees", temperature);
}Use signed integers (i8, i16, etc.) when you need negative numbers.
Use unsigned integers (u8, u16, etc.) when you only need zero or positive numbers.
Choosing the right size (8, 16, 32, 64, 128) depends on how big your numbers can get.
Integer types store whole numbers without decimals.
Signed integers can be negative; unsigned cannot.
Pick the size and sign based on your number needs.
Practice
Solution
Step 1: Understand signed vs unsigned integers
Signed integers (i32) store negative and positive numbers, unsigned (u32) store only zero and positive numbers.Step 2: Identify the type that stores only positive whole numbers
u32is unsigned 32-bit integer, so it stores only positive whole numbers including zero.Final Answer:
u32 -> Option AQuick Check:
Unsigned integer = u32 [OK]
- Confusing signed and unsigned types
- Choosing floating-point types for whole numbers
- Thinking bool is an integer type
x with value 100 in Rust?Solution
Step 1: Check Rust syntax for typed variable declaration
Rust useslet variable_name: type = value;syntax.Step 2: Identify the correct 16-bit signed integer type
i16is the 16-bit signed integer type in Rust.Final Answer:
let x: i16 = 100; -> Option BQuick Check:
Typed variable declaration with i16 = let x: i16 = 100; [OK]
- Using unsigned type
u16instead of signed - Using invalid type name
int16 - Incorrect syntax like missing colon
let a: i8 = 127;
let b: i8 = 1;
let c = a.wrapping_add(b);
println!("{}", c);Solution
Step 1: Understand i8 max value and wrapping_add behavior
The max value fori8is 127. Adding 1 causes overflow.wrapping_addwraps around instead of panicking.Step 2: Calculate the wrapped result
127 + 1 wraps to -128 ini8range.Final Answer:
-128 -> Option CQuick Check:
Wrapping addition overflow wraps to -128 [OK]
- Expecting 128 which is out of i8 range
- Thinking it causes runtime panic
- Confusing wrapping_add with normal addition
let x: u8 = -5;
Solution
Step 1: Check the type and value compatibility
u8is unsigned 8-bit integer and cannot hold negative values.Step 2: Identify the error cause
Assigning-5tou8causes a type mismatch error.Final Answer:
Negative value assigned to unsigned integer -> Option DQuick Check:
Unsigned integer cannot hold negative values [OK]
- Ignoring negative sign allowed in unsigned types
- Thinking semicolon is missing
- Assuming variable name is invalid
Solution
Step 1: Understand the size limits of integer types
u64max is about 18 quintillion, which cannot hold 20 quintillion.i64max is about 9 quintillion.u128is necessary.Step 2: Choose the type that safely stores 20 quintillion
u128can store very large positive integers up to 3.4e38, safely holding 20 quintillion.Final Answer:
u128 -> Option AQuick Check:
Use u128 for very large positive integers [OK]
- Choosing signed types unnecessarily
- Using i32 which is too small
- Assuming u64 is always enough without checking limits
Ready to prove it?
Pass the test and this pattern moves unknown
