【发布时间】:2015-08-23 05:01:44
【问题描述】:
我本周开始使用 Rust 编程,但在理解字符串的工作原理时遇到了很多问题。
现在,我正在尝试做一个简单的程序,打印一个附加订单的玩家列表(仅用于学习目的)。
let res : String = pl.name.chars().enumerate().fold(String::new(),|res,(i,ch)| -> String {
res+=format!("{} {}\n",i.to_string(),ch.to_string());
});
println!("{}", res);
这是我的想法,我知道我可以只使用 for 循环,但目标是了解不同的迭代器函数。
所以,我的问题是字符串连接不起作用。
Compiling prueba2 v0.1.0 (file:///home/pancho111203/projects/prueba2)
src/main.rs:27:13: 27:16 error: binary assignment operation `+=` cannot be applied to types `collections::string::String` and `collections::string::String` [E0368]
src/main.rs:27 res+=format!("{} {}\n",i.to_string(),ch.to_string());
^~~
error: aborting due to previous error
Could not compile `prueba2`.
我尝试使用 &str,但无法从 i 和 ch 值创建它们。
【问题讨论】:
-
这可能不是真正的重复,但正如所问的那样,它似乎是一样的。随时更新您的标题和问题,以更好地反映为什么它不是重复的,我们可以取消标记它。