gpt4 book ai didi

string - 正则表达式捕获不会像我认为的那样长

转载 作者:行者123 更新时间:2023-11-29 07:55:23 24 4
gpt4 key购买 nike

我正在尝试编写一个 Rust 函数,它接受一个正则表达式和一个字符串/str,并返回该正则表达式中所有命名捕获的 HashMap。这是代码:

use std::collections::HashMap;
use regex::Regex;

fn get_matches<'a>(line: &'a str, re: &Regex) -> HashMap<&'a str, &'a str> {
let mut results = HashMap::new();

match re.captures(line) {
None => { return results; },
Some(caps) => {
for (name, value) in caps.iter_named() {
if let Some(value) = value {
results.insert(name, value);
}
}
}
}

results
}

我得到这个编译器错误(Rust 1.9.0):

error: `caps` does not live long enough
for (name, value) in caps.iter_named() {
^~~~
note: reference must be valid for the lifetime 'a as defined on the block at 6:79...
fn get_matches<'a>(line: &'a str, re: &Regex) -> HashMap<&'a str, &'a str> {
let mut results = HashMap::new();

match re.captures(line) {
None => { return results; },
Some(caps) => {
...
note: ...but borrowed value is only valid for the match at 9:8
match re.captures(line) {
None => { return results; },
Some(caps) => {
for (name, value) in caps.iter_named() {
if let Some(value) = value {
results.insert(name, value);
...

但是,我不明白。 regex::Regex::captures return value has a lifetime of 't, which is the same lifetime as the string ,在这种情况下,这意味着 'a'regex::Captures::iter_named returned value also has the same lifetime of 't ,在本例中是 'a,这意味着 (name, value) for that thing也应该是 ',在本例中是 'a

我的函数定义有一个 HashMap,它使用那个 'a 生命周期,所以它不应该全部 Just Work(tm) 吗?我想我明白为什么你不能使用局部变量,除非你返回它,但在这种情况下,我使用的引用应该足够长,对吧?

我想我可以.clone() 一切到String,但我很好奇我是否可以只用引用来写这个。这不是应该更有效率吗?我对 Rust 有点陌生,所以我正在尝试以一种适当的、先进的方式理解事物并做事。

最佳答案

你的推理是对的,但是你忘记了一个细节:

regex::Regex::captures return value has a lifetime of 't, which is the same lifetime as the string, in this case, that means 'a, the regex::Captures::iter_named* returned value also has the same lifetime of 't, which is 'a in this case, and that means the (name, value) for that thing should also be 't, which in this case is 'a.

* regex::Captures::iter_named还需要 &'t self , 即 &caps必须有生命周期 't (在本例中为 'a)。

请注意,编译器不会提示 results但关于 caps . regex::Regex::captures返回 caps: Captures<'a> ,这意味着 caps 拥有生命周期的东西 'a .但是要打电话regex::Captures::iter_named有必要有一个生命周期的引用'a ( iter_named 参数为 &'a self = &'a Captures<'a> )。 虽然caps拥有终生的东西 'a , 它没有生命周期 'a (生命周期只有 Some 臂)。


我不知道如何iter_named使用空名称处理捕获,但这里是一个仅返回命名捕获的实现:

extern crate regex;

use std::collections::HashMap;
use regex::Regex;

fn get_matches<'a>(line: &'a str, re: &'a Regex) -> HashMap<&'a str, &'a str> {
let mut results = HashMap::new();

match re.captures(line) {
None => {
return results;
}
Some(caps) => {
for name in re.capture_names() {
if let Some(name) = name {
if let Some(value) = caps.name(name) {
results.insert(name, value);
}
}
}
}
}

results
}

这可能比 iter_named 慢.

关于string - 正则表达式捕获不会像我认为的那样长,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37993013/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com