std-method
String::insert_str
Insert a whole text fragment at a byte boundary without consuming the fragment. Expected stdout (not executed): ID: 42 Common mistake: Using an index greater than the byte length.
Formal Syntax & Structure
rust syntaxrust
fn insert_str(&mut self, idx: usize, string: &str)Working Example
Example Snippetrust
fn main() { let mut text = String::from("ID42"); text.insert_str(2, ": "); println!("{text}");}