regex - 迭代正则表达式捕获的终身问题
问题描述
我正在尝试使用正则表达式从字符串中获取所有非空白字符,但我一直回到同样的错误。
extern crate regex; // 1.0.2
use regex::Regex;
use std::vec::Vec;
pub fn string_split<'a>(s: &'a String) -> Vec<&'a str> {
let mut returnVec = Vec::new();
let re = Regex::new(r"\S+").unwrap();
for cap in re.captures_iter(s) {
returnVec.push(&cap[0]);
}
returnVec
}
pub fn word_n(s: &String, n: i32) -> &str {
let bytes = s.as_bytes();
let mut num = 0;
let mut word_start = 0;
for (i, &item) in bytes.iter().enumerate() {
if item == b' ' || item == b'\n' {
num += 1;
if num == n {
return &s[word_start..i].trim();
}
word_start = i;
continue;
}
}
&s[..]
}
错误:
error[E0597]: `cap` does not live long enough
--> src/main.rs:11:25
|
11 | returnVec.push(&cap[0]);
| ^^^ borrowed value does not live long enough
12 | }
| - borrowed value only lives until here
|
note: borrowed value must be valid for the lifetime 'a as defined on the function body at 6:1...
--> src/main.rs:6:1
|
6 | pub fn string_split<'a>(s: &'a String) -> Vec<&'a str> {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
加上更多信息:
$ rustc --explain E0597
This error occurs because a borrow was made inside a variable which has a
greater lifetime than the borrowed one.
Example of erroneous code:
```
struct Foo<'a> {
x: Option<&'a u32>,
}
let mut x = Foo { x: None };
let y = 0;
x.x = Some(&y); // error: `y` does not live long enough
```
In here, `x` is created before `y` and therefore has a greater lifetime. Always
keep in mind that values in a scope are dropped in the opposite order they are
created. So to fix the previous example, just make the `y` lifetime greater than
the `x`'s one:
```
struct Foo<'a> {
x: Option<&'a u32>,
}
let y = 0;
let mut x = Foo { x: None };
x.x = Some(&y);
```
在这一点上,我已经尝试了几种延长cap
变量生命周期的方法,但是在阅读了 Rust 书的借用和生命周期部分后,我无法得到任何工作。
解决方案
(这是您的代码中的)的文档说:impl<'t> Index<usize> for Captures<'t>
cap[0]
如果使用此方法,文本不能超过 Captures 对象,因为 Index 是如何定义的(通常 a[i] 是 a 的一部分并且不能超过它);为此,请改用 get()。
因此,get
它可以工作(请注意,我已将&'a String
参数替换为&'a str
):
use regex::Regex;
pub fn string_split<'a>(s: &'a str) -> Vec<&'a str> {
let mut return_vec = Vec::new();
let re = Regex::new(r"\S+").unwrap();
for cap in re.captures_iter(s) {
return_vec.push(cap.get(0).unwrap().as_str());
};
return_vec
}
fn main() {
println!("{:?}", string_split("Hello, world!"));
}
推荐阅读
- docker - 使用请求/响应模型的 docker 中的 Masstransit,请求消费者异常,响应时找不到主机
- sql-server - SSIS 包在部署 SQL Server 2012 后出现错误
- asp.net-web-api - 检查 Web API 的响应总是返回 500 错误(服务器上没有错误)
- html - Safari 渲染不同边框宽度的 bug
- ansible - 在 Ansible 任务 jinja 模板中循环访问主机时,不会使用这些主机的 host_vars
- javascript - 在两个事件监听器之间:如何在不使用 trigger() 的情况下传递我的函数?
- android - 将 REST API 提供的 JSON 保存到 android 上的本地文件
- c++ - CUDA:cudaMallocManaged 是否处理退出?
- google-oauth - 如何验证 Google OAuth session_state 参数返回到重定向 URI
- python - Python OpenCV 脚本通过 SSH 运行(失败请求的 X 错误:BadRequest)