0
0
Rustprogramming~5 mins

Character type in Rust - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What is the Rust char type used for?
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
Which of these is a valid Rust character literal?
A"a"
B'ab'
C"ab"
D'a'
What does the Rust char type represent?
AA single Unicode scalar value
BA byte of data
CA string of characters
DAn integer number
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>
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
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.