考虑以下功能:
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);
}
Run Code Online (Sandbox Code Playgroud)
如何取最后一个println!并将其转换为a "Welcome to the castle {}".to_string();并{}替换为name(显然我需要添加-> String到函数声明中.)
使用format!宏.
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)
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
54 次 |
| 最近记录: |