char_replace/
char_replace.rs

1use mut_str::StrExt;
2
3fn main() {
4    let mut s = Box::<str>::from("👋🌍");
5    println!("Original string:       {s:?}"); // "👋🌍"
6
7    // Get a mutable reference to the second character
8    let world = s.get_char_mut(1).unwrap();
9    println!("Original character:    {world:?}"); // '🌍'
10
11    // Replace '🌍' with 'w' and '_' as padding
12    // '🌍' is 4 bytes long and 'w' is 1, so 3 '_'s will be added after.
13    world.replace_with_pad_char('w', '_').unwrap();
14    println!("Replacement character: {world:?}"); // 'w'
15
16    println!("Final string:          {s:?}"); // "👋w___"
17}
18
19// Test the example (this can be ignored)
20#[test]
21fn test() {
22    main();
23}