本周我开始用Rust编程,我在理解字符串如何工作方面遇到了很多问题.
现在,我正在try 做一个简单的程序,打印一个球员名单,并附上他们的订单(仅供学习之用).
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);
这是我的 idea ,我知道我可以使用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
个值创建它们.