英文:
How can I do an explicit declaration of std::str::Matches<'a, P> in Rust?
问题
以下是您要翻译的内容:
假设我想对 Matches<'a, P> 类型进行显式声明。
除了使用 _ 之外,我可以传递哪些实际类型作为 'a?
 use std::str::Matches;
fn main() {
    let my_string = "Hello. I have a string and a substring.";
    let pat = "ing";
    let matches: Matches<'_, &'static str> = my_string.matches(pat);
    println!("{}", matches.count());
}
请注意,我已经删除了 HTML 编码和转义字符以使内容更清晰。
英文:
Suppose, I want to do an explicit declaration of Matches<'a, P> type.
What actual types can I pass for 'a rather than using _?
 use std::str::Matches;
fn main() {
    let my_string = "Hello. I have a string and a substring.";
    let pat = "ing";
    let matches: Matches<'_, &'static str> = my_string.matches(pat);
    println!("{}", matches.count());
}
答案1
得分: 1
对于 Matches<'a, P>,你需要 where P: Pattern<'a>。所以在你的情况下,应该是 'static:
let matches: Matches<'static, &'static str> = my_string.matches(pat);
英文:
For Matches<'a, P>, you have where P: Pattern<'a>. So in your case, it would be 'static:
let matches: Matches<'static, &'static str> = my_string.matches(pat);
答案2
得分: 1
这取决于你如何创建对象。对于你的情况,你正在使用 matches 函数来创建 Matches。
让我们检查一下 matches 的定义:
pub fn matches<'a, P>(&'a self, pat: P) -> Matches<'a, P>
where
    P: Pattern<'a>,
当你调用 my_string.matches(pat); 时,'a 就成了 my_string 的生命周期。(my_string 是 &'static str 那么 'a 就是 'static)
如果你将 my_string 类型设为 String,它将是任意生命周期。
在你的情况下,你无法显式声明生命周期,它将从 my_string 中获取,但考虑一个这样的函数:
use std::str::Matches;
fn count_ings<'a>(source: &'a str) -> usize {
    let matches: Matches<&'a str> = source.matches("ing"); // 显式声明
    matches.count()
}
fn main() {
    let my_string = "Hello. I have a string and a substring.".to_string();
    println!("'ing' count: {}", count_ings(&my_string));
}
在我看来,这给 matches 变量带来了一些轻微的保护,但如果你想用它,就用吧。你甚至可以选择不声明生命周期,无需添加通配符,感谢生命周期省略。
let matches: Matches<_> = source.matches("ing"); // 替换为上面的行。
英文:
It depends on how you create the object. For your case you are using matches function to create Matches .
Let's check the definition of matches:
pub fn matches<'a, P>(&'a self, pat: P) -> Matches<'a, P>
where
    P: Pattern<'a>,
When you call my_string.matches(pat); then 'a becomes the lifetime of my_string. (my_string is &'static str then 'a will be'static)
It would be arbitrary lifetime when my_string typed as String.
In your case you cannot explicitly declare lifetime, it will be fetched from my_string,
but consider a function like this:
use std::str::Matches;
fn count_ings<'a>(source: &'a str) -> usize {
    let matches: Matches<&'a str> = source.matches("ing"); //explicitly declared
    matches.count()
}
fn main() {
    let my_string = "Hello. I have a string and a substring.".to_string();
    println!("'ing' count: {}", count_ings(&my_string));
}
IMHO this brings minor protection to a matches var, but if you want to use it, just use it. Also you can even opt out lifetime declaration, there is no need to add a wild card thanks to lifetime elision.
let matches: Matches<_> = source.matches("ing"); //replace with the line above.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论