Bird
Raised Fist0
Rustprogramming~10 mins

Integer types in Rust - Step-by-Step Execution

Choose your learning style10 modes available
Concept Flow - Integer types
Start
↓
Choose integer type
↓
Assign value
↓
Check value fits type range?
No→Error: overflow
Yes↓
Use integer in program
↓
End
This flow shows how Rust integer types are chosen, assigned values, checked for range, and then used safely in programs.
Execution Sample
Rust
fn main() {
    let x: i8 = 100;
    let y: u8 = 200;
    println!("x = {}, y = {}", x, y);
}
This code declares two integers with different types and prints their values.
Execution Table
StepActionVariableValueCheckOutput
1Declare x as i8xuninitializedN/A
2Assign 100 to xx100100 fits in i8 (-128 to 127)
3Declare y as u8yuninitializedN/A
4Assign 200 to yy200200 fits in u8 (0 to 255)
5Print valuesx,y100, 200N/Ax = 100, y = 200
6End program
💡 Program ends after printing values successfully.
Variable Tracker
VariableStartAfter Step 2After Step 4Final
xuninitialized100100100
yuninitializeduninitialized200200
Key Moments - 2 Insights
Why can't we assign 130 to an i8 variable?
Because i8 can only hold values from -128 to 127. Assigning 130 would cause an overflow error as shown in step 2 where the check confirms the value fits.
What happens if we assign a negative number to a u8 variable?
u8 only holds values from 0 to 255. Assigning a negative number would cause a compile-time error. Step 4 shows the check for valid range.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the value of x after step 2?
A100
B-100
Cuninitialized
DOverflow error
💡 Hint
Check the 'Value' column for variable x at step 2 in the execution table.
At which step does the program print the values of x and y?
AStep 3
BStep 4
CStep 5
DStep 6
💡 Hint
Look for the 'Print values' action in the execution table.
If we assign 300 to y (u8), what would happen?
AValue wraps around without error
BCompile-time error due to overflow
CValue stored as 300 successfully
DRuntime error when printing
💡 Hint
Refer to the 'Check' column in the execution table for valid ranges of u8.
Concept Snapshot
Integer types in Rust specify size and sign, e.g., i8 (-128 to 127) and u8 (0 to 255).
Assigning values outside the range causes compile-time errors.
Use type annotations like let x: i8 = 100; to declare.
Rust checks values fit type ranges to prevent overflow.
Printing shows stored values after successful assignment.
Full Transcript
This visual execution trace shows how Rust handles integer types. We start by declaring variables with specific integer types like i8 and u8. Each variable is assigned a value, and Rust checks if the value fits within the allowed range for that type. For example, i8 can hold values from -128 to 127, so assigning 100 is valid. u8 holds 0 to 255, so 200 is valid. If a value does not fit, Rust will give an error to prevent overflow. Finally, the program prints the values of the variables. This step-by-step trace helps beginners see how integer types work and why range checks are important.

Practice

(1/5)
1. Which Rust integer type can store only positive whole numbers including zero?
easy
A. u32
B. i32
C. f32
D. bool

Solution

  1. Step 1: Understand signed vs unsigned integers

    Signed integers (i32) store negative and positive numbers, unsigned (u32) store only zero and positive numbers.
  2. Step 2: Identify the type that stores only positive whole numbers

    u32 is unsigned 32-bit integer, so it stores only positive whole numbers including zero.
  3. Final Answer:

    u32 -> Option A
  4. Quick Check:

    Unsigned integer = u32 [OK]
Hint: Unsigned integers start with 'u' and hold only positive numbers [OK]
Common Mistakes:
  • Confusing signed and unsigned types
  • Choosing floating-point types for whole numbers
  • Thinking bool is an integer type
2. Which of the following is the correct way to declare a 16-bit signed integer variable named x with value 100 in Rust?
easy
A. let x: u16 = 100;
B. let x: i16 = 100;
C. let x i16 = 100;
D. let x: int16 = 100;

Solution

  1. Step 1: Check Rust syntax for typed variable declaration

    Rust uses let variable_name: type = value; syntax.
  2. Step 2: Identify the correct 16-bit signed integer type

    i16 is the 16-bit signed integer type in Rust.
  3. Final Answer:

    let x: i16 = 100; -> Option B
  4. Quick Check:

    Typed variable declaration with i16 = let x: i16 = 100; [OK]
Hint: Use 'let var: i16 = value;' for 16-bit signed integers [OK]
Common Mistakes:
  • Using unsigned type u16 instead of signed
  • Using invalid type name int16
  • Incorrect syntax like missing colon
3. What will be the output of this Rust code?
let a: i8 = 127;
let b: i8 = 1;
let c = a.wrapping_add(b);
println!("{}", c);
medium
A. 128
B. 0
C. -128
D. Overflow error

Solution

  1. Step 1: Understand i8 max value and wrapping_add behavior

    The max value for i8 is 127. Adding 1 causes overflow. wrapping_add wraps around instead of panicking.
  2. Step 2: Calculate the wrapped result

    127 + 1 wraps to -128 in i8 range.
  3. Final Answer:

    -128 -> Option C
  4. Quick Check:

    Wrapping addition overflow wraps to -128 [OK]
Hint: wrapping_add wraps on overflow, not error [OK]
Common Mistakes:
  • Expecting 128 which is out of i8 range
  • Thinking it causes runtime panic
  • Confusing wrapping_add with normal addition
4. Identify the error in this Rust code snippet:
let x: u8 = -5;
medium
A. Missing semicolon
B. Type u8 does not exist
C. Variable name invalid
D. Negative value assigned to unsigned integer

Solution

  1. Step 1: Check the type and value compatibility

    u8 is unsigned 8-bit integer and cannot hold negative values.
  2. Step 2: Identify the error cause

    Assigning -5 to u8 causes a type mismatch error.
  3. Final Answer:

    Negative value assigned to unsigned integer -> Option D
  4. Quick Check:

    Unsigned integer cannot hold negative values [OK]
Hint: Unsigned types cannot hold negative numbers [OK]
Common Mistakes:
  • Ignoring negative sign allowed in unsigned types
  • Thinking semicolon is missing
  • Assuming variable name is invalid
5. You need to store the number of stars in the Milky Way galaxy, approximately 20,000,000,000,000,000,000 (20 quintillion). Which Rust integer type should you choose to store this number safely?
hard
A. u128
B. i64
C. u64
D. i32

Solution

  1. Step 1: Understand the size limits of integer types

    u64 max is about 18 quintillion, which cannot hold 20 quintillion. i64 max is about 9 quintillion. u128 is necessary.
  2. Step 2: Choose the type that safely stores 20 quintillion

    u128 can store very large positive integers up to 3.4e38, safely holding 20 quintillion.
  3. Final Answer:

    u128 -> Option A
  4. Quick Check:

    Use u128 for very large positive integers [OK]
Hint: Use u128 for very large positive integers beyond u64 [OK]
Common Mistakes:
  • Choosing signed types unnecessarily
  • Using i32 which is too small
  • Assuming u64 is always enough without checking limits
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard