Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

范围表达式

....= 运算符将根据下表构造 std::ops::Range (或 core::ops::Range) 变体之一的对象:

示例:

#![allow(unused)]
fn main() {
1..2;   // std::ops::Range
3..;    // std::ops::RangeFrom
..4;    // std::ops::RangeTo
..;     // std::ops::RangeFull
5..=6;  // std::ops::RangeInclusive
..=7;   // std::ops::RangeToInclusive
}

以下表达式是等价的。

#![allow(unused)]
fn main() {
let x = std::ops::Range {start: 0, end: 10};
let y = 0..10;

assert_eq!(x, y);
}

范围可以用于 for 循环:

#![allow(unused)]
fn main() {
for i in 1..11 {
    println!("{}", i);
}
}