Logo

Programming-Idioms

History of Idiom 219 > diff from v20 to v21

Edit summary for version 21 by programming-idioms.org:
[Rust] Comments: Replaces all whitespaces.

Version 20

2020-11-17, 14:48:31

Version 21

2020-11-22, 22:28:45

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 t = re.replace_all(s, " ");
Code
let re = Regex::new(r"\s+").unwrap();
let t = re.replace_all(s, " ");
Comments bubble
Replaces all whitespaces.
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=3c6a38c4dce2a3df4db98f80f6b7e61a
Demo URL
https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&gist=3c6a38c4dce2a3df4db98f80f6b7e61a