0

I have the following:

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);
    }
}

I am getting the warning:

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`

This is not intentional because I want to modify x and y in each iteration.


How do I shadow variables in the for-loop?

Kevin
  • 3,096
  • 2
  • 8
  • 37
  • You could do `let xy = add_one(&x, &y); x = xy.0; y=xy.1;` but the truth is this `add_one` function doesn't make sense. Why take two values and return a tuple ? You should probably define a type for this tuple. – Denys Séguret Jul 08 '21 at 16:20
  • Terminology: what you're currently doing is called shadowing: *creating a new variable with the same name*. So you're asking for the opposite. – kmdreko Jul 08 '21 at 16:21
  • 1
    Does this answer your question? [Can I destructure a tuple without binding the result to a new variable in a let/match/for statement?](https://stackoverflow.com/questions/34304341/can-i-destructure-a-tuple-without-binding-the-result-to-a-new-variable-in-a-let) – kmdreko Jul 08 '21 at 16:25

1 Answers1

0

Following the advice given by Denys Séguret I now have following solution:

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);
    }
}
Kevin
  • 3,096
  • 2
  • 8
  • 37
  • As a newcomer to Rust, any particular reason to not define `add_one` in an implementation of `Point`, such that `point = add_one(point)` can be `point = point.add_one()`? – Chris Jul 08 '21 at 18:50