© The Author(s), under exclusive license to APress Media, LLC, part of Springer Nature 2022
C. MilanesiBeginning Rusthttps://doi.org/10.1007/978-1-4842-7208-4_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, both right-exclusive and right-inclusive

  • How to define and use slices of arrays, vectors, or other slices, using ranges

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; that ...

Get Beginning Rust: Get Started with Rust 2021 Edition 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.