w3resource

Rust Program: Define character variable with first initial

Rust Variables and Data Types: Exercise-9 with Solution

Write a Rust program that defines a character variable and assigns it the first letter of your first name.

Sample Solution:

Rust Code:

fn main() {
    // Declare a character variable named 'temp' and assign it the first letter of your first name
    let temp: char = 'A';

    // Print the value of the 'first_initial' variable
    println!("First initial: {}", temp);
}

Output:

First initial: A

Explanation:

Here is a brief explanation of the above Rust code:

  • 'fn main() { ... }': This is the program's entry point.
  • 'let first_initial: char = 'A';': This line declares a character variable named 'temp' and assigns it the value ''A'', which represents the first letter of your first name. The 'char' type annotation specifies that 'temp' is a character variable.
  • 'println!("First initial: {}", temp);': This line prints the value of the 'temp' variable, displaying the first initial.

Rust Code Editor:

Previous: Rust Program: Iterate over array and print elements.
Next: Rust Program: Create vector of mixed items.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Follow us on Facebook and Twitter for latest update.