How to trim null bytes from String?

Hi. everyone!

let nft_type = match &nft_name[..] {
    "Primitive" => Some(0),
    "Uncommon" => Some(1),
    "Rare" => Some(2),
    "Legendary" => Some(3),
    _ => None
};

Here, nft_name is String.
The result of &nft_name[..] is
"Primitive\u{0}\u{0}\u{0}\u{0}\u{0}\u{0}\u{0}\u{0}\u{0}\u{0}\u{0}\u{0}\u{0}\u{0}\u{0}\u{0}"

So the match result is None
How can I get "Primitive" ?

Try trim_end_matches:

let nft_type = match nft_name.trim_end_matches('\0') {
    /* ... */
}

Playground.

1 Like

Thanks!