This is about Rust, regex::Regex.
For the following my code, I tried to output the input word followed by a random string.
Playground
The first function compiles but I don't want it because it does not use the random string.
The second function yields a compiler errors.
Could you give me how to fix my code?
use regex::Regex;
fn main() {
let cd="rust";
ok_but_i_dont_want_it(cd);
compiler_err(cd);
}
fn ok_but_i_dont_want_it(cd:&str){
let random_str="j93bg8";
let reg_idnt=Regex::new(r"(?P<ident>[_A-Za-z]{1,}[_A-Za-z0-9]{0,})").unwrap();
let cd=reg_idnt.replace_all(" rust ","${ident}_{}");
println!("{}",cd);
}
fn compiler_err(cd:&str){
let random_str="j93bg8";
let reg_idnt=Regex::new(r"(?P<ident>[_A-Za-z]{1,}[_A-Za-z0-9]{0,})").unwrap();
let cd=reg_idnt.replace_all(cd,format!("${ident}_{}",random_str));
println!("{}",cd);
}
error: there is no argument named `ident`
--> src/main.rs:18:47
|
18 | let cd=reg_idnt.replace_all(cd,format!("${ident}_{}",random_str));
| ^^^^^^^
error[E0277]: expected a `FnMut<(®ex::Captures<'_>,)>` closure, found `String`
question from:
https://stackoverflow.com/questions/65855968/in-rust-how-to-do-replace-all-regex-rep-of-regex-when-rep-contains-some-var 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…