Skip to content

Commit 51efc6f

Browse files
committed
Implement remove for String
1 parent 644653b commit 51efc6f

File tree

1 file changed

+35
-0
lines changed

1 file changed

+35
-0
lines changed

src/string.rs

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -255,6 +255,34 @@ impl<const N: usize> String<N> {
255255
Some(ch)
256256
}
257257

258+
/// Removes a [`u8`] from this `String` at a byte position and returns it.
259+
///
260+
/// Note: Because this shifts over the remaining elements, it has a
261+
/// worst-case performance of *O*(*n*).
262+
///
263+
/// # Panics
264+
///
265+
/// Panics if `idx` is larger than or equal to the `String`'s length,
266+
/// or if it does not lie on a [`char`] boundary.
267+
///
268+
/// # Examples
269+
///
270+
/// Basic usage:
271+
///
272+
/// ```
273+
/// use heapless::String;
274+
///
275+
/// let mut s: String<8> = String::from("foo");
276+
///
277+
/// assert_eq!(s.remove(0), b'f');
278+
/// assert_eq!(s.remove(1), b'o');
279+
/// assert_eq!(s.remove(0), b'o');
280+
/// ```
281+
#[inline]
282+
pub fn remove(&mut self, index: usize) -> u8 {
283+
self.vec.remove(index)
284+
}
285+
258286
/// Truncates this `String`, removing all contents.
259287
///
260288
/// While this means the `String` will have a length of zero, it does not
@@ -708,4 +736,11 @@ mod tests {
708736
assert_eq!(0, s.len());
709737
assert_eq!(8, s.capacity());
710738
}
739+
740+
#[test]
741+
fn remove() {
742+
let mut s: String<8> = String::from("foo");
743+
assert_eq!(b'f', s.remove(0));
744+
assert_eq!("oo", s.as_str());
745+
}
711746
}

0 commit comments

Comments
 (0)