-2

I have that does the following type of recursion and works as expected:

fn test2(i: isize) -> (isize) {
    println!("i={}", i);
    return (i + 1);
}

fn main() {
    let mut i = 1;
    let mut j = 1;
    for t in 0..4 {
        (i) = test2(i);
    }
}
i=1
i=2
i=3
i=4

The problem I have is what I really want is this recursion with multiple feedbacks like this.....

fn test(i: isize, j: isize) -> (isize, isize) {
    println!("i={} j={}", i, j);
    return (i + 1, j + 1);
}

fn main() {
    for t in 0..4 {
        let (i, j) = test(i, j);
        println!("ii {} jj {}", i, j);
    }
}

When I run the code I get the below:

i=1 j=1
ii 2 jj 2
i=1 j=1
ii 2 jj 2
i=1 j=1
ii 2 jj 2
i=1 j=1
ii 2 jj 2

If I leave off the let I get the below error:

for t in 0 .. 4 {
    (i,j) = test(i, j);
    println!("ii {} jj {}", i,j)  ; 
}
        error[E0070]: invalid left-hand side expression                                                                                                                                                          
   --> src/main.rs:265:13                                                                                                                                                                                
    |                                                                                                                                                                                                    
265 |             (i,j) = test(i, j);                                                                                                                                                                    
    |             ^^^^^^^^^^^^^^^^^^ left-hand of expression not valid                

How do I resolve?

Shepmaster
  • 388,571
  • 95
  • 1,107
  • 1,366
Tampa
  • 75,446
  • 119
  • 278
  • 425
  • 2
    For what its worth, there's no recursion in your code. Recursion, by definition, occurs when a function calls itself, which does not occur in your code. – Silvio Mayolo Jan 22 '19 at 16:17
  • Please review how to create a [MCVE] and then [edit] your question to include it. The code you have provided uses variables that are not defined and thus cannot produce the output you claim it does. Try to produce something that reproduces your error on the [Rust Playground](https://play.rust-lang.org) or you can reproduce it in a brand new Cargo project. There are [Rust-specific MCVE tips](//stackoverflow.com/tags/rust/info) as well. – Shepmaster Jan 22 '19 at 16:20
  • @SilvioMayolo you're right from a programming standpoint, but from a _mathematical_ standpoint, `i` and `j` are defined recursively since their value at time `t` depends on their value at time `t-1` – Jmb Jan 23 '19 at 08:07
  • @jmb this is for a kalman filter. By definition a Kalman filter is recursive. The above was the minimal to represent the issue I was having with the recursive nature of a Kalman filter – Tampa Jan 23 '19 at 11:02

1 Answers1

2

This feature is not currently available in Rust. There is an open issue on the issue tracker relating to it. For now, you have to destructure by hand.

let (i1, j1) = test(i, j);
i = i1;
j = j1;
Shepmaster
  • 388,571
  • 95
  • 1,107
  • 1,366
Silvio Mayolo
  • 62,821
  • 6
  • 74
  • 116