【问题标题】:What's the best way to iterate through a pair and update the first element with the second one in Rust?在 Rust 中迭代一对并用第二个元素更新第一个元素的最佳方法是什么?
【发布时间】:2022-11-29 11:38:40
【问题描述】:

有没有办法做这样的事情:

let (a, b) in as.as_mut_iter().zip(&bs) {
  *a = b;
}

【问题讨论】:

    标签: rust


    【解决方案1】:
    fn main() {
        let mut ass = vec![1, 2, 3];
        let bs = vec![4, 5, 6];
        for (a, b) in ass.iter_mut().zip(&bs) {
          *a = *b;
        }
        
        dbg!(ass);
    
    }
    

    似乎工作https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=edad99b5cafa850203e93742057c2ac2

    【讨论】:

    • 这是您正在寻找的解决方案还是您正在寻找其他东西?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-04-09
    • 1970-01-01
    • 1970-01-01
    • 2018-11-26
    • 1970-01-01
    • 2015-07-26
    • 1970-01-01
    相关资源
    最近更新 更多