官术网_书友最值得收藏!

Writing a condition

Similar to other languages, Rust conditions are expressed with the if and else keywords:

let number1 = 24;
let number2 = 42;
if number1 > number2 {
    println!("{} > {}", number1, number2);
} else {
    println!("{} <= {}", number1, number2);
}

However, they do not require parentheses around the conditional expression. Also, this expression must be of the bool type: you cannot use a number as you would in other languages.

One particularity of Rust conditions, like many other constructs, is that they are expressions. The last expression of each branch is the value of this branch. Be careful though, the type of each branch must be the same. For instance, we can get the minimum number of the two numbers and put it into a variable:

let minimum =
    if number1 < number2 {
        number1
    } else {
        number2
    }; // Don't forget the semi-colon here.
主站蜘蛛池模板: 博兴县| 瑞昌市| 宁波市| 宁波市| 汽车| 雷波县| 隆回县| 平远县| 白城市| 荣昌县| 兖州市| 平阳县| 丽江市| 海门市| 枣庄市| 合江县| 会宁县| 沿河| 诸城市| 长宁县| 工布江达县| 宝丰县| 北碚区| 渝北区| 封开县| 双峰县| 贵阳市| 福泉市| 麻栗坡县| 西宁市| 南宫市| 抚顺县| 九龙城区| 镇坪县| 宝兴县| 宜君县| 承德市| 白河县| 日照市| 晋中市| 大方县|