< 返回版块

coco 发表于 2023-08-15 09:45

Tags:borrowed

代码如下:

#![allow(unused)]

#[derive(Debug, Clone)]
struct Hello {
    name: String
}

#[derive(Debug, Clone)]
struct Word {
    sex: i32
}

#[derive(Debug, PartialEq, Clone)]
enum Flag {
    A,
    B,
    C,
}

#[derive(Debug, Clone)]
enum Hi {
    Hello(Hello, Flag),
    Word(Word, Flag),
}

fn is_flag(v: Vec<Hi>, flag: Flag) -> bool {
    for v1 in v {
        let f = match v1 {
            Hi::Hello(_, f) => f,
            Hi::Word(_, f) => f,
        };
        if f == flag {
            return true;
        }
    }
    false
}

fn loop_op(v: &mut Vec<Hi>) {
    for v1 in v {
        match v1 {
            Hi::Hello(_, _) => {
                println!("{:#?}", v1);
            },
            Hi::Word(ref mut w, _) => {
                if is_flag(v.to_vec(), Flag::C) {
                    w.sex = 2;
                    println!("{:#?}", v1);
                } else {
                    println!("{:#?}", v1);
                }
            },
        }
    }
}

fn main() {
    let mut v = vec![];
    v.push(Hi::Hello(Hello{name:"zhang3".to_string()}, Flag::A));
    v.push(Hi::Word(Word{sex:0}, Flag::B));
    v.push(Hi::Word(Word{sex:1}, Flag::C));
    loop_op(&mut v);
}

这是我从业务代码中摘出来,模拟当前情况的代码。请大佬先忽略业务的合理性。现在碰到这种问题,卡了一天了。不知道怎么解决了。

playground: https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=d7bbe925d9c20909cfb59a12e076a0c6

评论区

写评论
hangj 2023-08-21 16:08

我也改了一版

fn is_flag(v: &Vec<Hi>, flag: Flag) -> bool {
    for v1 in v {
        let f = match v1 {
            Hi::Hello(_, f) => f,
            Hi::Word(_, f) => f,
        };
        if *f == flag {
            return true;
        }
    }
    false
}

fn loop_op(v: &mut Vec<Hi>) {
    let is_flag = is_flag(v, Flag::C);

    for v1 in v {
        match v1 {
            Hi::Hello(_, _) => {
                println!("{:#?}", v1);
            },
            Hi::Word(ref mut w, _) => {
                if is_flag {
                    w.sex = 2;
                    println!("{:#?}", v1);
                } else {
                    println!("{:#?}", v1);
                }
            },
        }
    }
}
Tensionteng 2023-08-18 17:33

个人理解是:

for v1 in v

是语法糖,实际上是v.into_iter(),这个方法会拿走所有权,所以下面的v.to_vec()会因为没有所有权而报错

araraloren 2023-08-15 15:10

业务的话,最好写的质量高一些...

fn loop_op(v: &mut Vec<Hi>) {
    for idx in v
        .iter()
        .enumerate()
        .filter(|(_, _)| is_flag(v, Flag::C))
        .map(|v| v.0)
        .collect::<Vec<usize>>()
    {
        match &mut v[idx] {
            hi @ Hi::Hello(_, _) => {
                println!("{:#?}", hi);
            }
            Hi::Word(ref mut w, _) => {
                w.sex = 2;
            }
        }
    }
}
作者 coco 2023-08-15 10:31

谢谢,在我的真实业务代码中按这样修改编译成功了。 我再对比之前错误学习下。非常感谢。

--
👇
ttyy: 下改是改过的代码: https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=f92f8f7bb8f7184f9109da8954ad12c9

ttyy 2023-08-15 10:03

下改是改过的代码: https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=f92f8f7bb8f7184f9109da8954ad12c9

1 共 5 条评论, 1 页