Exercise 1: Hello, Rust!
Problem Statement
Write a Rust program that:
- Asks the user for their name
- Prints a personalized greeting including their name
- Prints the current date
Learning Objectives
- Practice using the
println!macro - Learn how to get user input using
std::io - Format strings using Rust's formatting capabilities
Starter Code
use std::io;
fn main() {
// TODO: 1. Prompt the user for their name
// TODO: 2. Read the user's input
let mut name = String::new();
// TODO: 3. Print a personalized greeting
// BONUS: Print the current date
// Hint: You can use the chrono crate for this
}
How to Run Your Code
- First, modify the starter code in
01_hello_rust_starter.rsaccording to the requirements - Run your code from the bootcamp root directory with:
cargo run --bin module1_01
Expected Output
What is your name? Alice
Hello, Alice! Welcome to the Rust Bootcamp!
Today is [current date].
Tips
- Use
io::stdin().read_line(&mut name)to read user input - Remember to handle the
Resultreturned byread_line - To trim whitespace (like newlines) from the input, use
name.trim() - For the bonus, you'll need to add the chrono crate to your dependencies in Cargo.toml