]> git.lizzy.rs Git - rust.git/blob - src/doc/unstable-book/src/library-features/splice.md
Auto merge of #41433 - estebank:constructor, r=michaelwoerister
[rust.git] / src / doc / unstable-book / src / library-features / splice.md
1 # `splice`
2
3 The tracking issue for this feature is: [#32310]
4
5 [#32310]: https://github.com/rust-lang/rust/issues/32310
6
7 ------------------------
8
9 The `splice()` method on `Vec` and `String` allows you to replace a range
10 of values in a vector or string with another range of values, and returns
11 the replaced values.
12
13 A simple example:
14
15 ```rust
16 #![feature(splice)]
17 let mut s = String::from("α is alpha, β is beta");
18 let beta_offset = s.find('β').unwrap_or(s.len());
19
20 // Replace the range up until the β from the string
21 let t: String = s.splice(..beta_offset, "Α is capital alpha; ").collect();
22 assert_eq!(t, "α is alpha, ");
23 assert_eq!(s, "Α is capital alpha; β is beta");
24 ```