Ranges
Create a Range object using 2-3 numbers with a double dot (..
) between them:
start..end
Section titled “start..end”Range starting at
start
(inclusive) and ending atend
(exclusive)
start..end..step
Section titled “start..end..step”Range starting at
start
(inclusive) and ending atend
(exclusive) with an optional step-by value
start..=end
Section titled “start..=end”Range starting at
start
and ending atend
(both inclusive)
start..=end..step
Section titled “start..=end..step”Range starting at
start
(inclusive) and ending atend
(both inclusive) with an optional step-by value
Examples
Section titled “Examples”let my_range = 0..10let inclusive = 0..=10let even_inclusive = 0..=10.2let only_even = 0..10..2let only_odd = 1..10..2
Ranges can also be used to index strings:
let my_string = "Hello World"let slice = my_string[0..5]
println(slice) # prints Hello