© Carlo Milanesi 2018
Carlo MilanesiBeginning Rusthttps://doi.org/10.1007/978-1-4842-3468-6_15

15. Ranges and Slices

Carlo Milanesi1 
(1)
Bergamo, Italy
 
In this chapter, you will learn:
  • How to use closed ranges, and open-ended ranges

  • How to process portions of arrays or vectors using slices

The Ranges

We already saw a way to write a for loop :

for i in 0..12 { println!("{}", i); }

But there is another possible way to write it:

let dozen = 0..12;
for i in dozen { println!("{}", i); }

This shows that the 0..12 clause is not a part of the for statement syntax, but it is an expression, whose value can be assigned to a variable. And that value can be used in for statements. The type of such value is named “range”.

Here is some more code using a range:

Get Beginning Rust: From Novice to Professional now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.