Does Rust have an equivalent to Python's unichr() function? -


python has unichr() (or chr() in python 3) function takes integer , returns character unicode code point of number. rust have equivalent function?

more information unichr() can found here:

https://docs.python.org/2/library/functions.html#unichr

sure, though built-in operator as:

let c: char = 97 char; println!("{}", c);   // prints "a" 

note as operator works u8 numbers, else cause compilation error:

let c: char = 97u32 char;  // error: `u8` can cast `char`, not `u32` 

if need string (to emulate python function), use to_string():

let s: string = (97 char).to_string(); 

there char::from_u32 function:

use std::char; let c: option<char> = char::from_u32(97); 

it returns option<char> because not every number valid unicode code point - valid numbers 0x0000 0xd7ff , 0xe000 0x10ffff. function applicable larger set of values as char , can convert numbers larger 1 byte, providing access whole range of unicode code points.

i've compiled set of examples on playpen here.


Comments

Popular posts from this blog

Java 3D LWJGL collision -

spring - SubProtocolWebSocketHandler - No handlers -

methods - python can't use function in submodule -