0
0
Rustprogramming~5 mins

Character type in Rust

Choose your learning style9 modes available
Introduction

The character type lets you store a single letter, number, or symbol in your program. It helps you work with text one piece at a time.

When you want to store a single letter or symbol, like 'a' or '#'.
When you need to check or compare one character in a word or sentence.
When you want to read input from a user one character at a time.
When you want to loop through each character in a string.
When you want to store special characters like emojis or accented letters.
Syntax
Rust
let variable_name: char = 'a';

Use single quotes ' ' to write a character.

The char type stores Unicode characters, so it can hold letters, numbers, symbols, and emojis.

Examples
This stores the letter 'x' in a variable named letter.
Rust
let letter: char = 'x';
This stores the character '7', not the number 7.
Rust
let digit: char = '7';
This stores the symbol '#' as a character.
Rust
let symbol: char = '#';
This stores an emoji character.
Rust
let emoji: char = '๐Ÿ˜Š';
Sample Program

This program creates two character variables and prints them.

Rust
fn main() {
    let ch: char = 'R';
    println!("The character is: {}", ch);

    let heart: char = 'โค';
    println!("Here is a heart: {}", heart);
}
OutputSuccess
Important Notes

Remember to use single quotes for characters, not double quotes (which are for strings).

Characters can be letters, digits, symbols, or even emojis because Rust uses Unicode.

You cannot store more than one character in a char variable.

Summary

The char type stores a single Unicode character.

Use single quotes to write characters.

Characters can be letters, numbers, symbols, or emojis.