Logo

Programming-Idioms

History of Idiom 169 > diff from v88 to v89

Edit summary for version 89 by programming-idioms.org:
[Rust] +DemoURL

Version 88

2021-03-22, 08:59:02

Version 89

2021-03-24, 21:46:14

Idiom #169 String length

Assign to integer n the number of characters of string s.
Make sure that multibyte characters are properly handled.
n can be different from the number of bytes of s.

Idiom #169 String length

Assign to integer n the number of characters of string s.
Make sure that multibyte characters are properly handled.
n can be different from the number of bytes of s.

Variables
n,s
Variables
n,s
Extra Keywords
size characters chars number runes
Extra Keywords
size characters chars number runes
Code
let n = s.chars().count();
Code
let n = s.chars().count();
Comments bubble
This is kind of roundabout because the default len method gives bytes, not characters.
Comments bubble
This is kind of roundabout because the default len method gives bytes, not characters.
Doc URL
https://doc.rust-lang.org/std/str/struct.Chars.html
Doc URL
https://doc.rust-lang.org/std/str/struct.Chars.html
Demo URL
https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&gist=7024db00eb16f2bc93c7ccf3e2ec326d