生命周期引用的字符串的iter方法

时间:2019-06-27 03:42:36

标签: rust lifetime

我有以下代码:

#[derive(Debug, PartialEq, Eq, Copy, Clone)]
pub struct Trip<'a> {
    pub cod: &'a str,
}

#[derive(Debug, Clone, Copy)]
pub struct ami<'a> {
    pub tlc: &'a str,
}

impl<'a> Trip<'a> {
    fn new(road: &'a str) -> Trip {
        assert_eq!(
            3,
            road.trim().len(),
            "Triplet doesn't have three Nucleotides"
        );
        let trip = road[0..].iter().flat_map(|s| s.chars()).collect();
        Triplet { cod: trip }
    }
}

我遇到以下错误:

error[E0599]: no method named `iter` found for type `str` in the current scope
  --> src/gcode/gcode.rs:20:30
   |
20 |         let trip = [0..].iter()
   |                              ^^^^

据我所知,new关联函数接收以生命周期为基准的str作为输入参数,并输出带有Trip字段的cod结构。在此过程中,输入由迭代器使用。

我应该收到有关生命周期无效的错误,但是在当前范围内,我没有办法使用str吗?

这是为什么?

0 个答案:

没有答案