Exerpad
🔥 0
0 XP
Exercise 8 · Chapter: Ownership & BorrowingRequired+50 XP

Swap Values

Write a function called swap that takes mutable borrows of two i32 values and swaps them. Print before and after to show the swap worked.

Expected output:

Before: a=10, b=20
After: a=20, b=10

Hints:

  • Function signature: fn swap(a: &mut i32, b: &mut i32)
  • Use a temporary variable: let temp = *a;
  • Use *a and *b to read/write the values behind the references
rust
fn main() {
}
Output
Run your code to see the output here.