0
0
Rustprogramming~10 mins

Character type in Rust - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Character type
Declare char variable
Assign a single character
Use char in code (print, compare, etc.)
Program runs showing char output
This flow shows how a character variable is declared, assigned a single character, used in code, and then output.
Execution Sample
Rust
fn main() {
    let letter: char = 'A';
    println!("Character is: {}", letter);
}
This code declares a char variable with 'A' and prints it.
Execution Table
StepActionVariableValueOutput
1Declare variable letter of type charletteruninitialized
2Assign 'A' to letterletter'A'
3Call println! macro to print letterletter'A'Character is: A
4Program endsletter'A'
💡 Program ends after printing the character.
Variable Tracker
VariableStartAfter Step 2Final
letteruninitialized'A''A'
Key Moments - 2 Insights
Why do we use single quotes ' ' for char instead of double quotes " "?
In Rust, single quotes ' ' are used for single characters (char type), while double quotes " " are for strings. See execution_table step 2 where 'A' is assigned to letter.
Can a char hold more than one character?
No, a char holds exactly one Unicode scalar value. Trying to assign more than one character causes a compile error. This is why letter only holds 'A' in step 2.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 2, what is the value of variable letter?
A'A'
B"A"
Cuninitialized
D'AB'
💡 Hint
Check the 'Value' column for step 2 in execution_table.
At which step does the program print the character?
AStep 2
BStep 3
CStep 1
DStep 4
💡 Hint
Look at the 'Output' column in execution_table to find when output occurs.
If we assign "A" (double quotes) instead of 'A' (single quotes) to letter, what happens?
AIt works the same
Bletter holds multiple characters
CCompile error because "A" is a string, not a char
Dletter becomes uninitialized
💡 Hint
Recall key_moments about single vs double quotes and char vs string.
Concept Snapshot
Rust char type holds a single Unicode character.
Declare with let variable: char = 'X'; using single quotes.
Use char for single letters, digits, symbols.
Double quotes " " are for strings, not chars.
Print chars with println! macro.
Char is 4 bytes, supports Unicode.
Full Transcript
This example shows how to use the Rust character type 'char'. First, we declare a variable named letter of type char. Then we assign the single character 'A' to it using single quotes. Next, we print the character using println! macro. The execution table traces each step: declaration, assignment, printing, and program end. The variable tracker shows letter starts uninitialized, then holds 'A'. Key moments clarify that chars use single quotes and hold exactly one character. The visual quiz tests understanding of variable values, output timing, and char vs string differences. The snapshot summarizes char usage in Rust.