小编Lyn*_* Wu的帖子

如何避免此程序的 for 循环和 let 语句

在 Rust 中,如何避免编写 for 循环和 let 语句?该程序用于记录字符串出现在文本的哪一行以及出现在该行的位置。

我知道我可以使用迭代器和映射来消除它们,但是我刚刚接触了 Rust,我不知道如何编写它。

pub struct L{
    x: usize,
    y: usize,
}
pub fn foo (text: &str, string: &str)->Vec<L> {
    let mut r= Vec::new();
    let mut x=0;
    for line in text.lines(){
        for (y, _) in line.match_indices(string){
            r.push(L{
                    x : x,
                    y: y, })
        }
        x+=1;
    }
    r
}
Run Code Online (Sandbox Code Playgroud)

rust

2
推荐指数
1
解决办法
111
查看次数

标签 统计

rust ×1