Is it possible to declare the type of the variable in Rust for loops?

后端 未结 3 1078
清歌不尽
清歌不尽 2020-12-03 16:30

C++ example:

for (long i = 0; i < 101; i++) {
    //...
}

In Rust I tried:

for i: i64 in 1..100 {
    // ...
}
         


        
3条回答
  •  南方客
    南方客 (楼主)
    2020-12-03 17:20

    You can use an integer suffix on one of the literals you've used in the range. Type inference will do the rest:

    for i in 1i64..101 {
        println!("{}", i);
    }
    

提交回复
热议问题