File tree 2 files changed +41
-0
lines changed 2 files changed +41
-0
lines changed Original file line number Diff line number Diff line change
1
+ fn calc_idx ( i : usize ) -> usize {
2
+ ( i + i + 20 ) % 4
3
+ }
4
+
5
+ fn main ( ) {
6
+ let ns = [ 2 , 3 , 5 , 7 ] ;
7
+
8
+ for i in 3 ..10 {
9
+ println ! ( "{}" , ns[ i] ) ;
10
+ }
11
+
12
+ for i in 3 ..10 {
13
+ println ! ( "{}" , ns[ i % 4 ] ) ;
14
+ }
15
+
16
+ for i in 3 ..10 {
17
+ println ! ( "{}" , ns[ i % ns. len( ) ] ) ;
18
+ }
19
+
20
+ for i in 3 ..10 {
21
+ println ! ( "{}" , ns[ calc_idx( i) ] ) ;
22
+ }
23
+
24
+ for i in 3 ..10 {
25
+ println ! ( "{}" , ns[ calc_idx( i) % 4 ] ) ;
26
+ }
27
+ }
Original file line number Diff line number Diff line change
1
+ error: the loop variable `i` is only used to index `ns`.
2
+ --> $DIR/needless_range_loop.rs:8:5
3
+ |
4
+ 8 | / for i in 3..10 {
5
+ 9 | | println!("{}", ns[i]);
6
+ 10 | | }
7
+ | |_____^
8
+ |
9
+ = note: `-D needless-range-loop` implied by `-D warnings`
10
+ help: consider using an iterator
11
+ |
12
+ 8 | for <item> in ns.iter().take(10).skip(3) {
13
+ | ^^^^^^
14
+
You can’t perform that action at this time.
0 commit comments