Logo

Programming-Idioms

History of Idiom 63 > diff from v48 to v49

Edit summary for version 49 by programming-idioms.org:
[Rust] DemoURL with gist

Version 48

2020-05-14, 12:26:50

Version 49

2021-03-25, 17:50:06

Idiom #63 Replace fragment of a string

Assign to x2 the value of string x with all occurrences of y replaced by z.
Assume occurrences of y are not overlapping.

Idiom #63 Replace fragment of a string

Assign to x2 the value of string x with all occurrences of y replaced by z.
Assume occurrences of y are not overlapping.

Variables
x2,x,y,z
Variables
x2,x,y,z
Extra Keywords
substring substitute
Extra Keywords
substring substitute
Code
let x2 = x.replace(&y, &z);
Code
let x2 = x.replace(&y, &z);
Comments bubble
Returns a string slice (`&str`). Add `.to_string()` or `.to_owned()` to get a `String`.
Comments bubble
Returns a string slice (&str). Add .to_string() or .to_owned() to get a String.
Demo URL
https://play.rust-lang.org/?code=fn%20main()%20%7B%0A%20%20%20%20let%20x%20%3D%20%22lorem%20ipsum%20dolor%20lorem%20ipsum%22%3B%0A%20%20%20%20let%20y%20%3D%20%22lorem%22%3B%0A%20%20%20%20let%20z%20%3D%20%22LOREM%22%3B%0A%0A%20%20%20%20let%20x2%20%3D%20x.replace(%26y%2C%20%26z)%3B%0A%20%20%20%20%0A%20%20%20%20println!(%22%7B%7D%22%2C%20x2)%3B%0A%7D%0A&version=stable
Demo URL
https://play.rust-lang.org/?version=stable&mode=debug&edition=2015&gist=568c9ebbfe29dfa173d6de1bf19f0e51