rust-lang / book

The Rust Programming Language

Home Page:https://doc.rust-lang.org/book/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Show example of unicode character spelling for emoji?

chriskrycho opened this issue · comments

Hello Rust ,

I am reading chapter 2 on Character Types, the books shows

fn main() {
    let c = 'z';
    let z: char = 'ℤ'; // with explicit type annotation
    let heart_eyed_cat = '😻';
}

I am wondering if you can show the use of unicode since "Unicode Scalar Values range from U+0000 to U+D7FF and U+E000 to U+10FFFF inclusive. " is in the paragraph under the code block.

The example I tested out and was happy it works is
let emoji = "\u{1F603}";

Could this be added to the book?

Originally posted by @PythonCoderUnicorn in #3794 (comment)

@PythonCoderUnicorn thanks for the suggestion. Can you help me understand a bit why you want a dedicated example for the Unicode “spelling” of the emoji? Note that there are a bunch of ways of writing character literals we do not include in that short example, since you can also “spell” it with a binary literal and so on.

Whenever I work with emojis I often use the unicode approach to explicitly have the emoji print out.

The standard Rust way as shown, having an emoji copy and pasted in the code (also a method in other languages) is not my preferred way. To me that is an extra step, I have to go to an app , get the emoji and paste it in.

I was just curious if the example could show another way of doing things.

Am not familiar with the spelling out the binary literal method. Anyway, thanks for responding.

This way of constructing character literals is covered by the reference, and I think that's sufficient. The book is not aiming to be comprehensive. Thanks for the feedback though!