w3resource

Rust Greeting Program: Print Welcome Message with Your Name

Rust Variables and Data Types: Exercise-6 with Solution

Write a Rust program that defines a string variable name and assigns it your name. Print a greeting message using the name variable.

Sample Solution:

Rust Code:

fn main() {
    // Declare a string variable named 'name' and assign it your name
    let name: &str = "Hippolyte Yao";

    // Print a greeting message using the 'name' variable
    println!("Hello, {}! Welcome to Rust programming.", name);
}

Output:

Hello, Hippolyte Yao! Welcome to Rust programming.

Explanation:

Here's a brief explanation of the above Rust code:

  • 'fn main() { ... }': This is the program's entry point.
  • 'let name: &str = "John";': This line declares a string variable named 'name' and assigns it the value '"John"'. The '&str' type annotation specifies that 'name' is a string slice.
  • 'println!("Hello, {}! Welcome to Rust programming.", name);': This line prints a greeting message using the 'name' variable. The '{}' placeholder is replaced with the value of the 'name' variable.

Rust Code Editor:

Previous: Rust Program: Convert Celsius to Fahrenheit.
Next: Rust: Print Latitude and Longitude separately.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Become a Patron!

Follow us on Facebook and Twitter for latest update.

It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.

https://www.w3resource.com/rust/basic/rust-variables-and-data-types-exercise-6.php