Add hanoi but in Rust.

This commit is contained in:
Nicolás A. Ortega Froysa 2022-10-17 16:52:56 +02:00
parent 1e513fe8c3
commit 0d1f3c5a4e

35
2/SAD/hanoi.rs Normal file
View File

@ -0,0 +1,35 @@
use std::env;
fn print_hanoi(n:i32)
{
if n == 2
{
println!("1");
println!("2");
println!("1");
}
else
{
print_hanoi(n-1);
println!("{}", n);
print_hanoi(n-1);
}
}
fn main()
{
let args:Vec<String> = env::args().collect();
if args.len() != 2
{
println!("Need a number!");
std::process::exit(1);
}
let n:i32 = args[1].parse().unwrap();
if n < 2
{
println!("Need a number greater than 1!");
std::process::exit(1);
}
print_hanoi(n);
}