实现说明
replacen 只替换前 n 个匹配以函数 main为骨架。
常见问题
「replacen 只替换前 n 个匹配」这个示例怎么运行?
把它当完整程序编译/解释运行即可——不需要先安装任何东西。
核心语句
let log = "error at boot, error at scan, error at save";
代码示例
fn main() {
let log = "error at boot, error at scan, error at save";
// replacen limits replacements to the first n matches
println!("{}", log.replacen("error", "warn", 1));
println!("{}", log.replacen("error", "warn", 2));
// n beyond the match count replaces every occurrence, like replace
println!("{}", log.replacen("error", "warn", 10));
}