Skip to content

Ranges

Create a Range object using 2-3 numbers with a double dot (..) between them:

Range starting at start (inclusive) and ending at end (exclusive)

Range starting at start (inclusive) and ending at end (exclusive) with an optional step-by value

Range starting at start and ending at end (both inclusive)

Range starting at start (inclusive) and ending at end (both inclusive) with an optional step-by value

let my_range = 0..10
let inclusive = 0..=10
let even_inclusive = 0..=10.2
let only_even = 0..10..2
let 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