Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to swap two variables?

Tags:

rust

What is the closest equivalent Rust code to this Python code?

a, b = 1, 2 a, b = b, a + b 

I am trying to write an iterative Fibonacci function. I have Python code I want to convert to Rust. Everything is fine, except for the swap part.

def fibonacci(n):     if n < 2:         return n     fibPrev = 1     fib = 1     for num in range(2, n):         fibPrev, fib = fib, fib + fibPrev     return fib 
like image 959
Nsukami _ Avatar asked Aug 04 '15 00:08

Nsukami _


People also ask

How do you exchange two variables in Java?

Two variables can be swapped in one line in Java. This is done by using the given statement. x = x ^ y ^ (y = x); where x and y are the 2 variables.

How do you swap two numbers with a temporary variable?

Swap Numbers Using Temporary Variable In the above program, the temp variable is assigned the value of the first variable. Then, the value of the first variable is assigned to the second variable. Finally, the temp (which holds the initial value of first ) is assigned to second . This completes the swapping process.


1 Answers

When swapping variables, the most likely thing you want is to create new bindings for a and b.

fn main() {     let (a, b) = (1, 2);     let (b, a) = (a, a + b); } 

For your actual case, you want to modify the existing bindings. Rust 1.59 will stabilize destructuring assignment:

fn fibonacci(n: u64) -> u64 {     if n < 2 {         return n;     }     let mut fib_prev = 1;     let mut fib = 1;     for _ in 2..n {         (fib_prev, fib) = (fib, fib + fib_prev);     }     fib } 

Before Rust 1.59, you can use a temporary variable:

fn fibonacci(n: u64) -> u64 {     if n < 2 {         return n;     }     let mut fib_prev = 1;     let mut fib = 1;     for _ in 2..n {         let next = fib + fib_prev;         fib_prev = fib;         fib = next;     }     fib } 

You could also make it so that you mutate the tuple:

fn fibonacci(n: u64) -> u64 {     if n < 2 {         return n;     }     let mut fib = (1, 1);     for _ in 2..n {         fib = (fib.1, fib.0 + fib.1);     }     fib.1 } 

You may also be interested in swapping the contents of two pieces of memory. 99+% of the time, you want to re-bind the variables, but a very small amount of time you want to change things "in place":

fn main() {     let (mut a, mut b) = (1, 2);     std::mem::swap(&mut a, &mut b);      println!("{:?}", (a, b)); } 

Note that it's not concise to do this swap and add the values together in one step.

For a very concise implementation of the Fibonacci sequence that returns an iterator:

fn fib() -> impl Iterator<Item = u128> {     let mut state = [1, 1];     std::iter::from_fn(move || {         state.swap(0, 1);         let next = state.iter().sum();         Some(std::mem::replace(&mut state[1], next))     }) } 

See also:

  • Can I destructure a tuple without binding the result to a new variable in a let/match/for statement?
  • How can I swap in a new value for a field in a mutable reference to a structure?
like image 90
Shepmaster Avatar answered Sep 22 '22 23:09

Shepmaster