Rust 如何创建字符串而不是将其打印到标准输出?
考虑以下功能:Rust 如何创建字符串而不是将其打印到标准输出?,rust,Rust,考虑以下功能: 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?");
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);
}
最后一个println怎么取代码>并将其转换为“欢迎来到城堡{}”
并将{}
替换为名称
(显然,我需要将->字符串
添加到函数声明中。)使用宏
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)
}