FrankKair / polyglot-euler

📜 Project Euler solutions in various programming languages
MIT License
74 stars 14 forks source link

Refactor: Rust 036 004 #64

Closed FrankKair closed 6 years ago

FrankKair commented 6 years ago

Refactor

004

Instead of type annotating a constant, I let the compiler infer the type by giving collect a type.

// Then
let reversed: String = n.to_string().chars().rev().collect();

// Now
let reversed = n.chars().rev().collect::<String>();

036

Uses a function with pattern matching to call a general palindrome check function.

fn number_palindrome(n: i32, base: i8) -> bool {
    match base {
        2 => palindrome(format!("{:b}", n)),
        10 => palindrome(n.to_string()),
        _ => false,
    }
}