我想在 Rust 中做一个简单的字符串反转,但我似乎无法弄清楚要使用什么函数来做到这一点。
假设我有一个字符串:____x_xx__x
反转这个字符串将变成:xxxx_x__xx_
我试过:
//res is the string I want to invert
for mut c in res.as_slice().chars() {
c =
match c {
'x' => '_',
_ => 'x'
};
};
但这警告我永远不会读取值 c
,所以我猜我使用的 c
实际上不是对切片中字符的引用?
最佳答案
fn main() {
let mut res = String::from_str("____x_xx__x").into_ascii();
for c in res.mut_iter() {
*c = match c.to_char() {
'x' => '_',
_ => 'x'
}.to_ascii();
};
println!("{}", res.into_string());
}
关于rust - 在 Rust 中反转字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24877699/