- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试使用正则表达式从字符串中获取所有非空白字符,但我总是遇到同样的错误。
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 书的借用和生命周期部分后,我无法得到任何工作.
最佳答案
documentation of impl<'t> Index<usize> for Captures<'t>
(这是您代码中的 cap[0]
)说:
The text can't outlive the Captures object if this method is used, because of how Index is defined (normally a[i] is part of a and can't outlive it); to do that, use get() instead.
所以 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!"));
}
关于regex - 迭代正则表达式捕获的生命周期问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51834111/
我正在开发一个使用多个 turtle 的滚动游戏。玩家 turtle 根据按键命令在 Y 轴上移动。当危害和好处在 X 轴上移动时,然后循环并改变 Y 轴位置。我尝试定义一个名为 colliding(
我不明白为什么他们不接受这个作为解决方案,他们说这是一个错误的答案:- #include int main(void) { int val=0; printf("Input:- \n
我正在使用基于表单的身份验证。 我有一个注销链接,如下所示: 以及对应的注销方法: public String logout() { FacesContext.getCurren
在 IIS7 应用程序池中有一个设置 Idle-time out 默认是 20 分钟,其中说: Amount of time(in minutes) a worker process will rem
我是一名优秀的程序员,十分优秀!