Skip to content

Commit

Permalink
Rollup merge of rust-lang#85801 - WaffleLapkin:master, r=joshtriplett
Browse files Browse the repository at this point in the history
Add `String::extend_from_within`

This PR adds `String::extend_from_within` function under the `string_extend_from_within` feature gate similar to the [`Vec::extend_from_within`] function.

```rust
// String
pub fn extend_from_within<R>(&mut self, src: R)
where
    R: RangeBounds<usize>;
```

[`Vec::extend_from_within`]: rust-lang#81656
  • Loading branch information
GuillaumeGomez authored May 30, 2021
2 parents 2d30bc7 + 23f9b92 commit b0f2a4c
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 0 deletions.
1 change: 1 addition & 0 deletions library/alloc/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,7 @@
#![feature(associated_type_bounds)]
#![feature(slice_group_by)]
#![feature(decl_macro)]
#![feature(bindings_after_at)]
// Allow testing this library

#[cfg(test)]
Expand Down
36 changes: 36 additions & 0 deletions library/alloc/src/string.rs
Original file line number Diff line number Diff line change
Expand Up @@ -843,6 +843,42 @@ impl String {
self.vec.extend_from_slice(string.as_bytes())
}

/// Copies elements from `src` range to the end of the string.
///
/// ## Panics
///
/// Panics if the starting point or end point do not lie on a [`char`]
/// boundary, or if they're out of bounds.
///
/// ## Examples
///
/// ```
/// #![feature(string_extend_from_within)]
/// let mut string = String::from("abcde");
///
/// string.extend_from_within(2..);
/// assert_eq!(string, "abcdecde");
///
/// string.extend_from_within(..2);
/// assert_eq!(string, "abcdecdeab");
///
/// string.extend_from_within(4..8);
/// assert_eq!(string, "abcdecdeabecde");
/// ```
#[cfg(not(no_global_oom_handling))]
#[unstable(feature = "string_extend_from_within", issue = "none")]
pub fn extend_from_within<R>(&mut self, src: R)
where
R: RangeBounds<usize>,
{
let src @ Range { start, end } = slice::range(src, ..self.len());

assert!(self.is_char_boundary(start));
assert!(self.is_char_boundary(end));

self.vec.extend_from_within(src);
}

/// Returns this `String`'s capacity, in bytes.
///
/// # Examples
Expand Down

0 comments on commit b0f2a4c

Please sign in to comment.