Rust中特别的“区间”概念

dragon72463399

drake

Posted on May 31, 2023

Rust中特别的“区间”概念
  • 1、半开区间,用下面的运算符表示
..
Enter fullscreen mode Exit fullscreen mode
  • 2、闭区间,用下面的运算符表示
..=
Enter fullscreen mode Exit fullscreen mode
  • 3、举个迭代区间元素的列子
// Rust
fn main() {
for i in 1..=5 {
    println!("{}",i);
}
}
Enter fullscreen mode Exit fullscreen mode
# Python
for i in range(1,6):
    print(i)
Enter fullscreen mode Exit fullscreen mode
  • 4、含义:半开区间只包括区间起始值,不包括结束值;闭区间包括起始值和结束值
  • 5、Python中并不存在这样的运算符来区分闭区间和半开区间,Python中只有半开的概念(不包括结束值)
  • 6、学Rust后才明白对某个区间还能这么分,甚至写了好几年的Python都没听过“区间”这个词汇
💖 💪 🙅 🚩
dragon72463399
drake

Posted on May 31, 2023

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related