Rust Greeting Program: Print Welcome Message with Your Name
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.
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics