Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

character
utf1
length
rust
communitycreator

What is character.len_utf16() in Rust?

okwudili onyejiaku

Overview

A character is made of bytes in memory. Depending on the encoding, a character can take up to different bytes. With the len_uft16() method, we can get the number of bytes a character would take in memory if encoded in UTF-16.

Syntax

character.len_uft8()
Syntax for len_utf16() method

Parameters

The method does not take any parameters.

Return value

The value this method returns is an integer. It represents the number of bytes the character would take in memory if encoded in UTF-16.

Example

fn main(){
  // check if some characters are really digits in some radix
    println!("{}", 'ß'.len_utf16());  // false
    println!("{}", '𝕊'.len_utf16());   // true
    println!("{}", '💣'.len_utf16());   // true
    println!("{}", 'c'.len_utf16());  // true
    println!("{}", 'ℝ'.len_utf16());  // true
    println!("{}", '.'.len_utf16());  // false
}
Get the number of bytes a character would take if encoded in UTF-16

Explanation

  • Line 4–8: We use the len_utf16() function to obtain the bytes of some characters encoded to UTF-16. Then we print these number of bytes to the console.

RELATED TAGS

character
utf1
length
rust
communitycreator
RELATED COURSES

View all Courses

Keep Exploring