如何在Rust中明确声明std::str::Matches<'a, P>?

huangapple go评论68阅读模式
英文:

How can I do an explicit declaration of std::str::Matches<'a, P> in Rust?

问题

以下是您要翻译的内容:

假设我想对 Matches&lt;&#39;a, P&gt; 类型进行显式声明。

除了使用 _ 之外,我可以传递哪些实际类型作为 &#39;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&lt;&#39;a, P&gt; type.

What actual types can I pass for &#39;a rather than using _?

 use std::str::Matches;

fn main() {
    let my_string = &quot;Hello. I have a string and a substring.&quot;;
    let pat = &quot;ing&quot;;

    let matches: Matches&lt;&#39;_, &amp;&#39;static str&gt; = my_string.matches(pat);

    println!(&quot;{}&quot;, matches.count());
}

答案1

得分: 1

对于 Matches&lt;&#39;a, P&gt;,你需要 where P: Pattern&lt;&#39;a&gt;。所以在你的情况下,应该是 &#39;static

let matches: Matches&lt;&#39;static, &amp;&#39;static str&gt; = my_string.matches(pat);
英文:

For Matches&lt;&#39;a, P&gt;, you have where P: Pattern&lt;&#39;a&gt;. So in your case, it would be &#39;static:

let matches: Matches&lt;&#39;static, &amp;&#39;static str&gt; = my_string.matches(pat);

答案2

得分: 1

这取决于你如何创建对象。对于你的情况,你正在使用 matches 函数来创建 Matches

让我们检查一下 matches 的定义:

pub fn matches&lt;&#39;a, P&gt;(&amp;&#39;a self, pat: P) -&gt; Matches&lt;&#39;a, P&gt;
where
    P: Pattern&lt;&#39;a&gt;,

当你调用 my_string.matches(pat); 时,&#39;a 就成了 my_string 的生命周期。(my_string&amp;&#39;static str 那么 &#39;a 就是 &#39;static)

如果你将 my_string 类型设为 String,它将是任意生命周期

在你的情况下,你无法显式声明生命周期,它将从 my_string 中获取,但考虑一个这样的函数:

use std::str::Matches;

fn count_ings&lt;&#39;a&gt;(source: &amp;&#39;a str) -&gt; usize {
    let matches: Matches&lt;&amp;&#39;a str&gt; = 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&lt;_&gt; = 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&lt;&#39;a, P&gt;(&amp;&#39;a self, pat: P) -&gt; Matches&lt;&#39;a, P&gt;
where
    P: Pattern&lt;&#39;a&gt;,

When you call my_string.matches(pat); then &#39;a becomes the lifetime of my_string. (my_string is &amp;&#39;static str then &#39;a will be&#39;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&lt;&#39;a&gt;(source: &amp;&#39;a str) -&gt; usize {
    let matches: Matches&lt;&amp;&#39;a str&gt; = source.matches(&quot;ing&quot;); //explicitly declared

    matches.count()
}

fn main() {
    let my_string = &quot;Hello. I have a string and a substring.&quot;.to_string();

    println!(&quot;&#39;ing&#39; count: {}&quot;, count_ings(&amp;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&lt;_&gt; = source.matches(&quot;ing&quot;); //replace with the line above.

huangapple
  • 本文由 发表于 2023年6月8日 22:44:29
  • 转载请务必保留本文链接:https://go.coder-hub.com/76433043.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定