std-method

str::replacen

Build a String replacing at most count matches from the beginning. Expected stdout (not executed): high high low Common mistake: Assuming count is a byte limit.

Formal Syntax & Structure

rust syntaxrust
fn replacen<P: Pattern>(&self, pat: P, to: &str, count: usize) -> String

Working Example

Example Snippetrust
fn main() {    let text = "low low low";    println!("{}", text.replacen("low", "high", 2));}
Rust Reference