Challenge - 5 Problems
Rust Character Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
โ Predict Output
intermediate2: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);
}Attempts:
2 left
๐ก Hint
Remember that chars can be cast to their ASCII numeric values.
โ Incorrect
The character 'A' has ASCII value 65. Adding 1 gives 66, which corresponds to 'B'.
โ Predict Output
intermediate2: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);
}Attempts:
2 left
๐ก Hint
Unicode escapes represent characters by their code points.
โ Incorrect
The Unicode code point U+2764 is the heart symbol โค, so it prints that.
๐ง Debug
advanced2: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);
}Attempts:
2 left
๐ก Hint
Check the types of s and c carefully.
โ Incorrect
A &str (string slice) cannot be assigned directly to a char because they are different types.
๐ Syntax
advanced2:00remaining
Which option correctly declares a char in Rust?
Choose the correct Rust syntax to declare a char variable holding the letter 'z'.
Attempts:
2 left
๐ก Hint
Chars use single quotes in Rust.
โ Incorrect
Chars are declared with single quotes and explicit type annotation is correct here.
๐ Application
expert2: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());
}Attempts:
2 left
๐ก Hint
Each char in Rust represents one Unicode scalar value regardless of byte length.
โ Incorrect
The array has exactly 4 chars: 'a', 'รฉ', '๐' (a musical symbol), and newline '\n'.