Logo

Programming-Idioms

History of Idiom 38 > diff from v12 to v13

Edit summary for version 13 by :

Version 12

2015-08-22, 16:01:54

Version 13

2015-08-22, 16:07:13

Idiom #38 Extract a substring

Find substring t consisting in characters i (included) to j (excluded) of string s.
(character indexes start at 0 unless specified otherwise)

Idiom #38 Extract a substring

Find substring t consisting in characters i (included) to j (excluded) of string s.
(character indexes start at 0 unless specified otherwise)

Imports
extern crate unicode_segmentation;
use unicode_segmentation::UnicodeSegmentation;
Code
let t = s.graphemes(true).skip(i).take(j - i).collect::<String>();
Comments bubble
Treats the 'character indexes' as indices of 'extended grapheme clusters' as defined by Unicode.
Doc URL
http://unicode-rs.github.io/unicode-segmentation/unicode_segmentation/trait.UnicodeSegmentation.html
Demo URL
https://play.rust-lang.org/?code=extern%20crate%20unicode_segmentation%3B%0Ause%20unicode_segmentation%3A%3AUnicodeSegmentation%3B%0A%0Afn%20main()%20%7B%0A%20%20%20%20let%20s%20%3D%20%22Lorem%20Ips%C3%BCm%20Dolor%22%3B%0A%20%20%20%20let%20(i%2C%20j)%20%3D%20(6%2C%2011)%3B%0A%20%20%20%20let%20t%20%3D%20s.graphemes(true).skip(i).take(j%20-%20i).collect%3A%3A%3CString%3E()%3B%0A%20%20%20%20println!(%22%7B%7D%22%2C%20t)%3B%0A%7D%0A&version=stable