What is character.len_utf8() in Rust?

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());
}

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.

Free Resources