我本周开始学习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
值创建它们。