Why we need "char" data type?

Rust’s char type is four bytes in size and represents a Unicode Scalar Value, which means it can represent a lot more than just ASCII. Accented letters; Chinese, Japanese, and Korean characters; emoji; and zero-width spaces are all valid char values in Rust. - The book

let japan = "商";

let japan: char = '商';

Both code can make an output > 商. So why we need char if string can do accented letters.

In which situation i must use char rather than string?

Thank you.

When processing "characters" for humans, it's best not to use char. Use grapheme clusters instead, which are short strings.

char exists in Rust as a technical compromise. Full proper Unicode support requires a lot of code and extra data tables, which would make the standard library much bigger, and it is too much for some of the platforms that Rust targets. So instead of full grapheme cluster processing, Rust has Unicode Scalar Values that are cheap and simple to get from UTF-8 strings. It's not a proper character, but at least it's not a byte. It can be a building block for other Unicode-aware algorithms.


Thanks, clearly explanation :grin:

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.