Bird
Raised Fist0
Rustprogramming~15 mins

Character type in Rust - Mini Project: Build & Apply

Choose your learning style10 modes available
Character type
๐Ÿ“– Scenario: You are creating a simple Rust program that works with characters. Characters in Rust are single Unicode scalar values, like letters or symbols.
๐ŸŽฏ Goal: Build a Rust program that stores a character, checks if it is a letter, and then prints the character and whether it is a letter.
๐Ÿ“‹ What You'll Learn
Create a variable of type char with a specific character
Create a boolean variable to check if the character is alphabetic
Use the is_alphabetic() method on the character
Print the character and the boolean result
๐Ÿ’ก Why This Matters
๐ŸŒ Real World
Working with characters is common in text processing, input validation, and building user interfaces.
๐Ÿ’ผ Career
Understanding character types and their methods is essential for Rust developers working on software that handles text or user input.
Progress0 / 4 steps
1
Create a char variable
Create a variable called my_char of type char and set it to the character 'R'.
Rust
Hint

Use single quotes for characters in Rust, like 'R'.

2
Check if the character is alphabetic
Create a boolean variable called is_letter and set it to the result of calling my_char.is_alphabetic().
Rust
Hint

Use the is_alphabetic() method on my_char to check if it is a letter.

3
Print the character and the check result
Use println! to print the message: Character: {my_char}, Is letter: {is_letter} using an f-string style format.
Rust
Hint

Use println!("Character: {}, Is letter: {}", my_char, is_letter); to print the values.

4
Run and see the output
Run the program and observe the output. It should print: Character: R, Is letter: true.
Rust
Hint

Check the console output after running the program.

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

  1. Step 1: Understand Rust char syntax

    Rust uses single quotes for char literals, e.g., 'a'. Double quotes are for strings.
  2. 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.
  3. Final Answer:

    let letter: char = 'a'; -> Option B
  4. 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

  1. Step 1: Recall char literal rules

    Rust char literals must be in single quotes, even for emojis.
  2. 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.
  3. Final Answer:

    let smiley: char = '๐Ÿ˜€'; -> Option A
  4. 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

  1. Step 1: Understand is_alphabetic() method

    This method returns true if the char is a letter (A-Z or a-z or other alphabets).
  2. Step 2: Check the character 'Z'

    'Z' is an uppercase letter, so is_alphabetic() returns true.
  3. Final Answer:

    true -> Option A
  4. 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

  1. Step 1: Check char literal rules

    A char can only hold one Unicode character, so 'ab' is invalid.
  2. Step 2: Analyze the error

    Using multiple characters inside single quotes causes a compile error.
  3. Final Answer:

    Multiple characters in single quotes are not allowed -> Option D
  4. 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

  1. 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.
  2. 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.
  3. Final Answer:

    let crab = s.chars().nth(5).unwrap(); -> Option C
  4. 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 unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard