从连接变量创建Unicode字符的预期方法是什么?

时间:2017-12-31 09:54:45

标签: rust

let mut start = true;
for v in count as String {
    s += match v as usize {
        n if n == 2 || n == 3 => "\u{00b" + n + "}",
        n if n >= 4 || (n <= 1 && !start) => "\u{207" + n + "}",
        start && 1 => "",
    }
    start = false;
}

我知道这不是编写Unicode字符的预期方法:

error: unterminated unicode escape (needed a `}`)
 --> src/main.rs:5:45
  |
5 |             n if n == 2 || n == 3 => "\u{00b" + n + "}",
  |                                             ^

但是正确的方法是什么?

1 个答案:

答案 0 :(得分:0)

结合std::char::from_u32String::push

use std::char::from_u32;
let mut s = String::new();
s.push(from_u32(0xb0 + 2).unwrap());
s.push(from_u32(0xb0 + 3).unwrap());
s.push(from_u32(0x2070 + 1).unwrap());
println!("{:?}", s);