The char type in Rust represents a single Unicode scalar value, which means it can hold any valid Unicode character, including letters, numbers, emojis, and symbols.
Click to reveal answer
beginner
How many bytes does a Rust char use in memory?
A Rust char uses 4 bytes (32 bits) in memory to store a Unicode scalar value.
Click to reveal answer
beginner
How do you write a character literal in Rust?
You write a character literal using single quotes, like 'a', '๐', or '\n' for newline.
Click to reveal answer
beginner
Can a Rust char hold multiple characters or strings?
No, a Rust char holds exactly one Unicode scalar value, which is one character. For multiple characters, use a String or string slice &str.
Click to reveal answer
intermediate
How can you convert a char to its numeric Unicode code point in Rust?
You can convert a <code>char</code> to a <code>u32</code> number using the <code>as</code> keyword, like <code>let code = 'A' as u32;</code>.
Click to reveal answer
What is the size in bytes of a Rust char?
A8 bytes
B1 byte
C4 bytes
D2 bytes
โ Incorrect
A Rust char always uses 4 bytes to store a Unicode scalar value.
Which of these is a valid Rust character literal?
A"a"
B'ab'
C"ab"
D'a'
โ Incorrect
Rust character literals use single quotes and hold exactly one character, like 'a'.
What does the Rust char type represent?
AA single Unicode scalar value
BA byte of data
CA string of characters
DAn integer number
โ Incorrect
The char type represents a single Unicode scalar value, which can be any valid Unicode character.
How do you convert a char to its Unicode code point number?
AUsing <code>char as u32</code>
BUsing <code>char.to_u32()</code>
CUsing <code>char.to_string()</code>
DUsing <code>char.to_int()</code>
โ Incorrect
You convert a char to a number with char as u32.
Can a Rust char hold an emoji character?
ANo, only ASCII characters are allowed
BYes, because emojis are Unicode scalar values
COnly if converted to a string first
DOnly if stored as bytes
โ Incorrect
Rust char can hold any Unicode scalar value, including emojis.
Explain what the Rust char type is and how it differs from a string.
Think about the difference between a single letter and a word.
You got /4 concepts.
Describe how to write and use a character literal in Rust.
Remember how characters are shown in code.
You got /4 concepts.
Practice
(1/5)
1. Which of the following correctly declares a char variable in Rust?
easy
A. let letter: char = `a`;
B. let letter: char = 'a';
C. let letter: char = a;
D. let letter: char = "a";
Solution
Step 1: Understand Rust char syntax
Rust uses single quotes for char literals, e.g., 'a'. Double quotes are for strings.
Step 2: Check each option
let letter: char = 'a'; uses single quotes correctly. let letter: char = "a"; uses double quotes (string). let letter: char = a; misses quotes. let letter: char = `a`; uses backticks, which is invalid.
Final Answer:
let letter: char = 'a'; -> Option B
Quick Check:
char uses single quotes [OK]
Hint: Remember: single quotes for char, double quotes for string [OK]
Common Mistakes:
Using double quotes instead of single quotes
Omitting quotes around the character
Using backticks instead of quotes
2. Which of the following is the correct way to assign the emoji ๐ to a char variable in Rust?
easy
A. let smiley: char = '๐';
B. let smiley: char = ๐;
C. let smiley: char = "๐";
D. let smiley: char = `๐`;
Solution
Step 1: Recall char literal rules
Rust char literals must be in single quotes, even for emojis.
Step 2: Evaluate options
let smiley: char = '๐'; uses single quotes correctly with the emoji. Double quotes ("๐") create a string. Bare ๐ lacks quotes. Backticks (`๐`) are invalid.
Final Answer:
let smiley: char = '๐'; -> Option A
Quick Check:
Emoji char uses single quotes [OK]
Hint: Use single quotes for any char, including emojis [OK]
Common Mistakes:
Using double quotes for char
Not quoting the emoji
Using backticks instead of single quotes
3. What will be the output of this Rust code?
let c: char = 'Z';
println!("{}", c.is_alphabetic());
medium
A. true
B. false
C. Z
D. Compile error
Solution
Step 1: Understand is_alphabetic() method
This method returns true if the char is a letter (A-Z or a-z or other alphabets).
Step 2: Check the character 'Z'
'Z' is an uppercase letter, so is_alphabetic() returns true.
Final Answer:
true -> Option A
Quick Check:
'Z'.is_alphabetic() = true [OK]
Hint: Check if char is letter with is_alphabetic() method [OK]
Common Mistakes:
Confusing output with printing the char itself
Thinking is_alphabetic() returns the char
Expecting a compile error
4. Identify the error in this Rust code snippet:
let ch: char = 'ab';
medium
A. Missing semicolon at the end
B. Use double quotes for multiple characters
C. char type cannot store letters
D. Multiple characters in single quotes are not allowed
Solution
Step 1: Check char literal rules
A char can only hold one Unicode character, so 'ab' is invalid.
Step 2: Analyze the error
Using multiple characters inside single quotes causes a compile error.
Final Answer:
Multiple characters in single quotes are not allowed -> Option D
Quick Check:
char holds one char only [OK]
Hint: char must be one character in single quotes [OK]
Common Mistakes:
Trying to store multiple characters in a char
Using double quotes for char
Ignoring compile errors
5. Given a string slice let s = "Rust ๐ฆ";, which Rust code correctly extracts the crab emoji as a char?
hard
A. let crab = s.chars().nth(4).unwrap();
B. let crab = s[5];
C. let crab = s.chars().nth(5).unwrap();
D. let crab = s[4..5].chars().next().unwrap();
Solution
Step 1: Understand string and char indexing in Rust
Rust strings are UTF-8 encoded. The string "Rust ๐ฆ" has 6 chars: 'R'(0),'u'(1),'s'(2),'t'(3),' '(4),'๐ฆ'(5). Crab emoji is the 6th character (index 5), counting chars, not bytes.
Step 2: Check each option
let crab = s.chars().nth(5).unwrap(); correctly uses chars().nth(5) to get the crab emoji. let crab = s.chars().nth(4).unwrap(); gets the space (index 4). let crab = s[5]; uses byte index (u8, not char). let crab = s[4..5].chars().next().unwrap(); byte slice yields space.
Final Answer:
let crab = s.chars().nth(5).unwrap(); -> Option C
Quick Check:
Use chars().nth(5) for crab emoji [OK]
Hint: Use chars().nth(index) to get char by position [OK]
Common Mistakes:
Indexing string bytes directly
Using wrong index for emoji
Not unwrapping Option from nth()
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.