W3cubDocs

/Rust

Function std::char::from_u32_unchecked

pub unsafe fn from_u32_unchecked(i: u32) -> char

Converts a u32 to a char, ignoring validity.

Note that all chars are valid u32s, and can be casted to one with as:

let c = '💯';
let i = c as u32;

assert_eq!(128175, i);

However, the reverse is not true: not all valid u32s are valid chars. from_u32_unchecked() will ignore this, and blindly cast to char, possibly creating an invalid one.

Safety

This function is unsafe, as it may construct invalid char values.

For a safe version of this function, see the from_u32() function.

Examples

Basic usage:

use std::char;

let c = unsafe { char::from_u32_unchecked(0x2764) };

assert_eq!('❤', c);

© 2010 The Rust Project Developers
Licensed under the Apache License, Version 2.0 or the MIT license, at your option.
https://doc.rust-lang.org/std/char/fn.from_u32_unchecked.html