Logo

Programming-Idioms

History of Idiom 38 > diff from v11 to v12

Edit summary for version 12 by :

Version 11

2015-08-22, 02:20:43

Version 12

2015-08-22, 16:01:54

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)

Code
let t=s[i,j].to_string();
Code
let t = s[i..j].to_string();
Comments bubble
Please note that 'character indexes' here are actually byte indexes – make sure you don't cut off a unicode grapheme!
Doc URL
http://doc.rust-lang.org/std/ops/trait.Index.html
Demo URL
https://play.rust-lang.org/?code=fn%20main()%20%7B%0A%20%20%20%20let%20s%20%3D%20%22Lorem%20Ipsum%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%5Bi..j%5D.to_string()%3B%0A%20%20%20%20println!(%22%7B%7D%22%2C%20t)%3B%0A%7D%0A&version=stable