Consider the following function:
use std::io;
pub fn hello() {
println!("Hello, How are you doing? What's your characters name?");
let mut name = String::new();
io::stdin().read_line(&mut name).expect("Failed to read name. What was that name again?");
println!("Welcome to the castle {}", name);
}
How do I take the last println!
and turn it into a "Welcome to the castle {}".to_string();
and have the {}
replaced with name
(obviously I would need to add -> String
to the function declaration.)
Use the format!
macro.
pub fn hello() -> String {
println!("Hello, How are you doing? What's your characters name?");
let mut name = String::new();
io::stdin().read_line(&mut name).expect("Failed to read name. What was that name again?");
format!("Welcome to the castle {}", name)
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With