diff options
| author | Ulrik Sverdrup <root@localhost> | 2015-05-01 15:34:25 +0200 |
|---|---|---|
| committer | Ulrik Sverdrup <root@localhost> | 2015-05-01 19:51:31 +0200 |
| commit | ee48e6d192166be08a57dc4f5ba14256c072e9c3 (patch) | |
| tree | ee1c9e1b9aefe2a1ba00f426061e27ab60cb8cc4 /src/libcollectionstest/string.rs | |
| parent | 42bfeec53c266fb0b08ad90d324206bd3d64df16 (diff) | |
| download | rust-ee48e6d192166be08a57dc4f5ba14256c072e9c3.tar.gz rust-ee48e6d192166be08a57dc4f5ba14256c072e9c3.zip | |
collections: Implement String::drain(range) according to RFC 574
`.drain(range)` is unstable and under feature(collections_drain). This adds a safe way to remove any range of a String as efficiently as possible. As noted in the code, this drain iterator has none of the memory safety issues of the vector version. RFC tracking issue is #23055
Diffstat (limited to 'src/libcollectionstest/string.rs')
| -rw-r--r-- | src/libcollectionstest/string.rs | 17 |
1 files changed, 17 insertions, 0 deletions
diff --git a/src/libcollectionstest/string.rs b/src/libcollectionstest/string.rs index d842d1e7f27..d4e2ebf4fd1 100644 --- a/src/libcollectionstest/string.rs +++ b/src/libcollectionstest/string.rs @@ -348,6 +348,23 @@ fn test_from_iterator() { assert_eq!(s, d); } +#[test] +fn test_drain() { + let mut s = String::from("αβγ"); + assert_eq!(s.drain(2..4).collect::<String>(), "β"); + assert_eq!(s, "αγ"); + + let mut t = String::from("abcd"); + t.drain(..0); + assert_eq!(t, "abcd"); + t.drain(..1); + assert_eq!(t, "bcd"); + t.drain(3..); + assert_eq!(t, "bcd"); + t.drain(..); + assert_eq!(t, ""); +} + #[bench] fn bench_with_capacity(b: &mut Bencher) { b.iter(|| { |
