Struct emojis::Emoji

source ·
pub struct Emoji { /* private fields */ }
Expand description

Represents an emoji.

See Unicode.org for more information.

Implementations§

source§

impl Emoji

source

pub const fn as_str(&self) -> &str

Returns this emoji as a string.

§Examples
let rocket = emojis::get("🚀").unwrap();
assert_eq!(rocket.as_str(), "🚀")
source

pub const fn as_bytes(&self) -> &[u8]

Returns this emoji as slice of UTF-8 encoded bytes.

§Examples
let rocket = emojis::get("🚀").unwrap();
assert_eq!(rocket.as_bytes(), &[0xf0, 0x9f, 0x9a, 0x80]);
Examples found in repository?
examples/replace.rs (line 42)
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
fn replace(mut s: &str, mut o: impl Write) -> io::Result<()> {
    // The meaning of the index values is as follows.
    //
    //  : r o c k e t :
    // ^ ^           ^ ^
    // i m           n j
    //
    // i..j gives ":rocket:"
    // m..n gives "rocket"
    while let Some((i, m, n, j)) = s
        .find(':')
        .map(|i| (i, i + 1))
        .and_then(|(i, m)| s[m..].find(':').map(|x| (i, m, m + x, m + x + 1)))
    {
        match emojis::get_by_shortcode(&s[m..n]) {
            Some(emoji) => {
                // Output everything preceding, except the first colon.
                o.write_all(s[..i].as_bytes())?;
                // Output the emoji.
                o.write_all(emoji.as_bytes())?;
                // Update the string to past the last colon.
                s = &s[j..];
            }
            None => {
                // Output everything preceding but not including the colon.
                o.write_all(s[..n].as_bytes())?;
                // Update the string to start with the last colon.
                s = &s[n..];
            }
        }
    }
    o.write_all(s.as_bytes())
}
source

pub const fn name(&self) -> &str

Returns the CLDR name for this emoji.

§Examples
let cool = emojis::get("😎").unwrap();
assert_eq!(cool.name(), "smiling face with sunglasses");
source

pub const fn unicode_version(&self) -> UnicodeVersion

Returns the Unicode version this emoji first appeared in.

§Examples
use emojis::UnicodeVersion;

let villain = emojis::get("🦹").unwrap();
assert_eq!(villain.unicode_version(), UnicodeVersion::new(11, 0));
source

pub const fn group(&self) -> Group

Returns the group this emoji belongs to.

§Examples
use emojis::Group;

let flag = emojis::get("🇿🇦").unwrap();
assert_eq!(flag.group(), Group::Flags);
source

pub fn skin_tone(&self) -> Option<SkinTone>

Returns the skin tone of this emoji.

§Examples
use emojis::SkinTone;

let peace = emojis::get("✌️").unwrap();
assert_eq!(peace.skin_tone(), Some(SkinTone::Default));

let peace = emojis::get("✌🏽").unwrap();
assert_eq!(peace.skin_tone(), Some(SkinTone::Medium));

For emojis where skin tones are not applicable this will be None.

let cool = emojis::get("😎").unwrap();
assert!(cool.skin_tone().is_none());
source

pub fn skin_tones(&self) -> Option<impl Iterator<Item = &Self> + Clone>

Returns an iterator over the emoji and all the related skin tone emojis.

§Examples
use emojis::Emoji;

let luck = emojis::get("🤞🏼").unwrap();
let skin_tones: Vec<_> = luck.skin_tones().unwrap().map(Emoji::as_str).collect();
assert_eq!(skin_tones, ["🤞", "🤞🏻", "🤞🏼", "🤞🏽", "🤞🏾", "🤞🏿"]);

Some emojis have 26 skin tones!

use emojis::SkinTone;

let couple = emojis::get("👩🏿‍❤️‍👨🏼").unwrap();
let skin_tones = couple.skin_tones().unwrap().count();
assert_eq!(skin_tones, 26);

For emojis where skin tones are not applicable this will return None.

let cool = emojis::get("😎").unwrap();
assert!(cool.skin_tones().is_none());
source

pub fn with_skin_tone(&self, skin_tone: SkinTone) -> Option<&Self>

Returns a version of this emoji that has the given skin tone.

§Examples
use emojis::SkinTone;

let raised_hands = emojis::get("🙌🏼")
    .unwrap()
    .with_skin_tone(SkinTone::MediumDark)
    .unwrap();
assert_eq!(raised_hands, emojis::get("🙌🏾").unwrap());
use emojis::SkinTone;

let couple = emojis::get("👩‍❤️‍👨")
    .unwrap()
    .with_skin_tone(SkinTone::DarkAndMediumLight)
    .unwrap();
assert_eq!(couple, emojis::get("👩🏿‍❤️‍👨🏼").unwrap());

For emojis where the skin tone is not applicable this will return None.

use emojis::SkinTone;

let cool = emojis::get("😎").unwrap();
assert!(cool.with_skin_tone(SkinTone::Medium).is_none());
source

pub fn shortcode(&self) -> Option<&str>

Returns the first GitHub shortcode for this emoji.

Most emojis only have zero or one shortcode but for a few there are multiple. Use the shortcodes() method to return all the shortcodes. See gemoji for more information.

For emojis that have zero shortcodes this will return None.

§Examples
let thinking = emojis::get("🤔").unwrap();
assert_eq!(thinking.shortcode().unwrap(), "thinking");
source

pub fn shortcodes(&self) -> impl Iterator<Item = &str> + Clone

Returns an iterator over the GitHub shortcodes for this emoji.

Most emojis only have zero or one shortcode but for a few there are multiple. Use the shortcode() method to return the first shortcode. See gemoji for more information.

For emojis that have zero shortcodes this will return an empty iterator.

§Examples
let laughing = emojis::get("😆").unwrap();
assert_eq!(
    laughing.shortcodes().collect::<Vec<_>>(),
    vec!["laughing", "satisfied"]
);

Trait Implementations§

source§

impl AsRef<[u8]> for Emoji

source§

fn as_ref(&self) -> &[u8]

Converts this type into a shared reference of the (usually inferred) input type.
source§

impl AsRef<str> for Emoji

source§

fn as_ref(&self) -> &str

Converts this type into a shared reference of the (usually inferred) input type.
source§

impl Debug for Emoji

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
source§

impl Display for Emoji

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
source§

impl Hash for Emoji

source§

fn hash<H: Hasher>(&self, state: &mut H)

Feeds this value into the given Hasher. Read more
1.3.0 · source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
source§

impl PartialEq<&str> for Emoji

source§

fn eq(&self, s: &&str) -> bool

This method tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

fn ne(&self, other: &Rhs) -> bool

This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
source§

impl PartialEq<str> for Emoji

source§

fn eq(&self, s: &str) -> bool

This method tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

fn ne(&self, other: &Rhs) -> bool

This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
source§

impl PartialEq for Emoji

source§

fn eq(&self, other: &Emoji) -> bool

This method tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

fn ne(&self, other: &Rhs) -> bool

This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
source§

impl Eq for Emoji

Auto Trait Implementations§

§

impl Freeze for Emoji

§

impl RefUnwindSafe for Emoji

§

impl Send for Emoji

§

impl Sync for Emoji

§

impl Unpin for Emoji

§

impl UnwindSafe for Emoji

Blanket Implementations§

source§

impl<T> Any for T
where T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for T
where T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for T
where U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.