0
0
Rustprogramming~10 mins

Integer types in Rust - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Integer types
Start
Choose integer type
Assign value
Check value fits type range?
NoError: 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.