0
0
Rustprogramming~20 mins

Character type in Rust - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
๐ŸŽ–๏ธ
Rust Character Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
โ“ Predict Output
intermediate
2:00remaining
What is the output of this Rust code using char?
Consider the following Rust code snippet. What will it print?
Rust
fn main() {
    let c: char = 'A';
    let n = c as u8 + 1;
    println!("{}", n as char);
}
AC
BB
CA
DError: cannot cast char to u8 directly
Attempts:
2 left
๐Ÿ’ก Hint
Remember that chars can be cast to their ASCII numeric values.
โ“ Predict Output
intermediate
2:00remaining
What does this Rust code print with Unicode char?
Look at this Rust code. What will it output?
Rust
fn main() {
    let heart = '\u{2764}';
    println!("{}", heart);
}
Aโค
B\u{2764}
CError: invalid Unicode escape
D?
Attempts:
2 left
๐Ÿ’ก Hint
Unicode escapes represent characters by their code points.
๐Ÿ”ง Debug
advanced
2:00remaining
Why does this Rust code fail to compile?
This Rust code tries to convert a string to a char but fails. Why?
Rust
fn main() {
    let s = "hello";
    let c: char = s;
    println!("{}", c);
}
AMissing semicolon after let c: char = s
BString "hello" is too long to convert to char
CCannot assign &str to char directly; types mismatch
Dchar type cannot be printed with println!
Attempts:
2 left
๐Ÿ’ก Hint
Check the types of s and c carefully.
๐Ÿ“ Syntax
advanced
2:00remaining
Which option correctly declares a char in Rust?
Choose the correct Rust syntax to declare a char variable holding the letter 'z'.
Alet c: char = "z";
Blet c: char = z;
Clet c = 'z';
Dlet c: char = 'z';
Attempts:
2 left
๐Ÿ’ก Hint
Chars use single quotes in Rust.
๐Ÿš€ Application
expert
2:00remaining
How many Unicode scalar values are in this Rust char array?
Given this Rust code, how many Unicode scalar values does the array contain?
Rust
fn main() {
    let chars: [char; 4] = ['a', 'รฉ', '๐„ž', '\n'];
    println!("{}", chars.len());
}
A4
B3
C5
DError: invalid char literal
Attempts:
2 left
๐Ÿ’ก Hint
Each char in Rust represents one Unicode scalar value regardless of byte length.