mem::swap(&mut x, &mut y);でswapできる。
https://doc.rust-lang.org/std/mem/fn.swap.html
use std::mem; fn main() { println!("二つの数字を入力してください。"); let mut x = String::new(); let mut y = String::new(); std::io::stdin().read_line(&mut x).expect("Failed to read line"); x = x.trim_end().to_owned(); std::io::stdin().read_line(&mut y).expect("Failed to read line"); y = y.trim_end().to_owned(); mem::swap(&mut x, &mut y); println!("入力した値をswapすると「{}」と「{}」です", x, y); }
Finished `dev` profile [unoptimized + debuginfo] target(s) in 0.39s
Running `target/debug/basic`
二つの数字を入力してください。
20
15
入力した値をswapすると「15」と「20」です
既に用意されているのね…