迭代正则表达式捕获的生命周期问题

chr*_*ism 1 regex lifetime object-lifetime rust borrowing

我尝试使用正则表达式从字符串中获取所有非空白字符,但我不断回到相同的错误。

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[..]
}
Run Code Online (Sandbox Code Playgroud)

错误:

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[..]
}
Run Code Online (Sandbox Code Playgroud)

加上更多信息:

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> {
   | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Run Code Online (Sandbox Code Playgroud)

此时,我已经尝试了几种延长变量生命周期的方法cap,但在阅读 Rust 书的借用和生命周期部分后,我无法让任何东西起作用。

sta*_*lue 5

(这是您的代码中的)的文档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!"));
}
Run Code Online (Sandbox Code Playgroud)