如何在 0.9 中反转字符串? [英] How do I reverse a string in 0.9?
本文介绍了如何在 0.9 中反转字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何在 Rust 0.9 中反转字符串?
How do I reverse a string in Rust 0.9?
根据 rosettacode.org 这在 0.8 中有效:
According to rosettacode.org this worked in 0.8:
let reversed:~str = "一二三四五六七八九十".rev_iter().collect();
...但我无法让迭代器在 0.9 中处理字符串.
... but I can't get iterators working on strings in 0.9.
也尝试过 std::str::StrSlice::bytes_rev
但我还没有想出一种干净的方法来将结果转换回字符串而不会导致编译器窒息.
Also tried std::str::StrSlice::bytes_rev
but I haven't figured out a clean way to convert the result back into a string without the compiler choking.
推荐答案
首先对字节进行迭代,逆向会破坏多字节字符(你想要对字符进行迭代)
First of all iteration over bytes and reversing will break multibyte characters (you want iteration over chars)
let s = ~"abc";
let s2: ~str = s.chars_rev().collect();
println!("{:?}", s2);
这篇关于如何在 0.9 中反转字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文