首页 > 解决方案 > 从元组赋值循环中隐藏变量

问题描述

我有以下内容:

use num_bigint::BigUint;

fn add_one(px: &BigUint, py: &BigUint) -> (BigUint, BigUint) {
    (px+1u32, py+1u32)
}

fn test(x: &[u8], y: &[u8]) {
    let x = BigUint::from_bytes_le(x);
    let y = BigUint::from_bytes_le(y);

    for _ in 1..5 {
        let (x,y) = add_one(&x, &y);
    }
}

我收到警告:

warning: unused variable: `x`
   --> src/lib.rs:109:14
    |
109 |         let (x,y) = ecc_add(&x, &y, &x, &y);
    |              ^ help: if this is intentional, prefix it with an underscore: `_x`

这不是故意的,因为我想在每次迭代中修改x和。y


如何在 for 循环中隐藏变量?

标签: rust

解决方案


按照 Denys Séguret 给出的建议,我现在有以下解决方案:

struct Point {
    x: BigUint,
    y: BigUint
}

fn add_one(p: Point) -> Point {
    Point {x: p.x + 1u32, y: p.y + 1u32}
}

fn test(x: &[u8], y: &[u8]) {
    let x = BigUint::from_bytes_le(x);
    let y = BigUint::from_bytes_le(y);

    let mut point = Point {x: x, y: y};

    for _ in 1..5 {
        point = add_one(point);
    }
}

推荐阅读