Regex::replace
有签名
pub fn replace<'t, R: Replacer>(&self, text: &'t str, rep: R) -> Cow<'t, str>
Replacer
执行人:
-
&'a str
-
ReplacerRef<'a, R> where R: Replacer
-
F where F: FnMut(&Captures) -> T, T: AsRef<str>
-
NoExpand<'t>
没有实现
String
,这是错误消息的直接原因。您可以通过转换
字符串
在字符串切片中:
replace_all(&input, &*"$1:".to_uppercase()
由于大写版本与小写版本相同,因此不会有任何更改。
但是,执行
替代品
以结束
是
有用的:
let result = regex_path_without_dot.replace_all(&input, |captures: ®ex::Captures| {
captures[1].to_uppercase() + ":"
});
replace_all(&input, "$1:".to_uppercase())
这显示了在理解此功能如何工作或在函数优先级方面的一个基本错误。这和说:
let x = "$1:".to_uppercase();
replace_all(&input, x)
或者,同样地,因为
1
是大写的吗
1个
和
$
是大写的吗
$
以下内容:
let x = String::from("$1:");
replace_all(&input, x)
调用类似于
to_uppercase
不会神奇地推迟到“晚些时候”。