1

I want to create an escaped unicode scalar value at runtime. How can I achieve this?

let omega = "3A9";
assert_eq!("\u{3A9}", format!("\\u{{{}}}", omega))

Error: thread 'main' panicked at 'assertion failed: (left == right) left: "Ω", right: "\\u{3A9}"'

1 Answer 1

4

You want to interpret the string as an hexadecimal integer and construct a char using it.

let omega = "3A9";
let code_point = u32::from_str_radix(omega, 16).unwrap();
let s = char::from_u32(code_point).unwrap().to_string();
Sign up to request clarification or add additional context in comments.

1 Comment

Worth noting, if you control the source of the omega variable, it would be better to store it as a u32 from the beginning: let omega = 0x3A9

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.