Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

uft8
rust
length
character
communitycreator

What is character.len_utf8() in Rust?

okwudili onyejiaku

Overview

In Ruby, the len_utf8() method returns the number of bytes a character would need if encoded in UFT-8. UTF-8 is the most common character encoding. It is popular because it can store text containing characters efficiently.

Syntax

character.len_uft8()
Syntax for len_utf8() method in Rust

Parameters

This method does not take any parameter.

Return value

In Rust, the total size of a character is 4 bytes. Hence, the return value is an integer between 1 and 4.

Example

fn main(){
  // get the number of bytes a character will take if
  // encoded in UFT-8
    println!("{}", '9'.len_utf8());   
    println!("{}", 'A'.len_utf8());   
    println!("{}", 'c'.len_utf8());  
    println!("{}", 'ℝ'.len_utf8());  
    println!("{}", 'πŸ’£'.len_utf8()); 
}
Get bytes for a character to be encoded in UFT-8

Explanation

  • Lines 4–8: We use the len_utf8() function to obtain the bytes of some characters which are encoded to UTF-8. Then we print these number of bytes to the console.

RELATED TAGS

uft8
rust
length
character
communitycreator
RELATED COURSES

View all Courses

Keep Exploring