idiombook
7/16/2017 - 2:44 PM

[language: Rust, statement: conditional]

[language: Rust, statement: conditional]

enum Weather {
    Sunny,
    Cloudy,
    Rainy
}

fn weather(rainy_percent: i32) -> Option<Weather> {
    return if 0 <= rainy_percent && rainy_percent < 30 {
        Some(Weather::Sunny)
    } else if 30 <= rainy_percent && rainy_percent < 70 {
        Some(Weather::Cloudy)
    } else if 70 <= rainy_percent && rainy_percent <= 100 {
        Some(Weather::Rainy)
    } else {
        None
    };
}

fn weather_result(weather: Option<Weather>) -> String {
    if let Some(weather) = weather {
        return match weather {
            Weather::Sunny => "晴れ".to_string(),
            Weather::Cloudy => "曇り".to_string(),
            Weather::Rainy => "雨".to_string()
        };
    } else {
        return "エラー".to_string()
    };
}

fn main() {
    println!("{}", weather_result(weather(29)));
    println!("{}", weather_result(weather(60)));
    println!("{}", weather_result(weather(90)));
    println!("{}", weather_result(weather(200)));
}