Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/rust/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Rust 如何创建字符串而不是将其打印到标准输出?_Rust - Fatal编程技术网

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)
}