Logo

Programming-Idioms

History of Idiom 219 > diff from v19 to v20

Edit summary for version 20 by programming-idioms.org:
[Rust] Variable names s, t

Version 19

2020-11-17, 00:43:26

Version 20

2020-11-17, 14:48:31

Idiom #219 Replace multiple spaces with single space

Create string t from the value of string s with each sequence of spaces replaced by a single space.

Explain if only the space characters will be replaced, or the other whitespaces as well: tabs, newlines.

Idiom #219 Replace multiple spaces with single space

Create string t from the value of string s with each sequence of spaces replaced by a single space.

Explain if only the space characters will be replaced, or the other whitespaces as well: tabs, newlines.

Extra Keywords
collapse repeated
Extra Keywords
collapse repeated
Imports
use regex::Regex;
Imports
use regex::Regex;
Code
let re = Regex::new(r"\s+").unwrap();
let s2 = re.replace_all(s1, " ");
Code
let re = Regex::new(r"\s+").unwrap();
let t = re.replace_all(s, " ");
Doc URL
https://docs.rs/regex/1.4.2/regex/#example-replacement-with-named-capture-groups
Doc URL
https://docs.rs/regex/1.4.2/regex/#example-replacement-with-named-capture-groups
Demo URL
https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&gist=c1fac19554db5841f6b4caf4b9919943
Demo URL
https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&gist=3c6a38c4dce2a3df4db98f80f6b7e61a