std-method

String::drain

Remove a UTF-8-aligned byte range and iterate over its removed scalars. The drain holds a mutable borrow until it is dropped. Expected stdout (not executed): ش AB Common mistake: Choosing range endpoints inside a multibyte scalar.

Formal Syntax & Structure

rust syntaxrust
text.drain(byte_range)

Working Example

Example Snippetrust
fn main() {    let mut s = String::from("AشB");    let removed: String = s.drain(1..3).collect();    println!("{removed} {s}");}
Rust Reference