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

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.
主站蜘蛛池模板: 双桥区| 綦江县| 张家界市| 土默特左旗| 高尔夫| 师宗县| 荥阳市| 青铜峡市| 武城县| 延安市| 依兰县| 湘乡市| 岑溪市| 甘孜| 翁牛特旗| 枣庄市| 林州市| 郧西县| 泌阳县| 股票| 外汇| 岐山县| 同德县| 霍城县| 望江县| 梅河口市| 宁河县| 荔波县| 海丰县| 剑阁县| 德惠市| 鄂尔多斯市| 元江| 安多县| 宁阳县| 普陀区| 蓬溪县| 和龙市| 天柱县| 喜德县| 印江|