Logo

Programming-Idioms

History of Idiom 63 > diff from v5 to v6

Edit summary for version 6 by :

Version 5

2015-08-20, 11:40:21

Version 6

2015-08-20, 11:54:27

Idiom #63 Replace fragment of a string

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

Idiom #63 Replace fragment of a string

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

Code
let x2=x.replace(&y,&z).to_string();
Code
let x2 = x.replace(&y, &z);
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