Range Operator in Rust with example

This tutorial explains the range of operators available in Rust programming.

The range operator, also known as two periods (..) in Rust, has the following syntax.

start .. end

where start is the initial value and end is the final value.

Range operators used in arrays and vectors to obtain a slice of an object.

Rust Range Operators

Rust provides several variations of range operators:

  • Range This operator includes the start value and excludes the end value. Its syntax is:
1..5

The returned values are 1, 2, 3, 4.

Here is an example

  fn main() {
    let numbers = [0, 1, 2, 3, 4,5,6,7,8,9];
    // array print
    println!("{:?}", numbers);
    println!("{:?}",&numbers[1..6] );
    println!("{:?}",&numbers[1..1] );
}

Output:

[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
[1, 2, 3, 4, 5]
[]
  • RangeFrom Operator

This operator includes the start value and omits the end value. Its syntax is.

1..

The returned values are 1, 2, 3, 4, and so on, indefinitely.

Here is an example

fn main() {
    let numbers = [0, 1, 2, 3, 4,5,6,7,8,9];
    // array print
    println!("{:?}", numbers);
    println!("{:?}",&numbers[1..] );
    println!("{:?}",&numbers[9..] );
}

Output:

[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
[1, 2, 3, 4, 5, 6, 7, 8, 9]
[9]
  • RangeTo Operator

This operator considers the end value, while the start value is omitted. The start value begins from 0, and the end value is end-1. Its syntax is.

..5

The returned values are 0, 1, 2, 3, 4, and so on.

Here is an example

fn main() {
    let numbers = [0, 1, 2, 3, 4,5,6,7,8,9];
    // array print
    println!("{:?}", numbers);
    println!("{:?}",&numbers[..5] );
    println!("{:?}",&numbers[..2] );
}

Output:

[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
[0, 1, 2, 3, 4]
[0, 1]
  • RangeFull Operator This operator considers both start and end values. The start value is 0, and the end value is end-1. Its syntax is:
..

The returned values are 0, 1, 2, 3, 4, and so on, indefinitely.

Here is an example

fn main() {
    let numbers = [0, 1, 2, 3, 4,5,6,7,8,9];
    // array print
    println!("{:?}", numbers);
    println!("{:?}",&numbers[..] );
}

Output:

[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
  • RangeInclusive Operator This operator includes the start and end values.

It includes equal operators such as start..=end Its syntax is

0..=4

The returned values are 0, 1, 2, 3, 4, and so on.

Here is an example

fn main() {
    let numbers = [0, 1, 2, 3, 4,5,6,7,8,9];
    // array print
    println!("{:?}", numbers);
    println!("{:?}",&numbers[1..=4] );
}

Output:

[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
[1, 2, 3, 4]
  • RangeToInclusive Operator This operator includes equal operators such as ..=end, and the start value is omitted.

The start value is considered zero. The end value is considered as the end. Its syntax is:

..=4

The returned values are 0, 1, 2, 3, 4, and so on.

Here is an example

fn main() {
    let numbers = [0, 1, 2, 3, 4,5,6,7,8,9];
    // array print
    println!("{:?}", numbers);
    println!("{:?}",&numbers[0..=4] );
}

Output:

[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
[1, 2, 3, 4]

Conclusion

This tutorial covers multiple range operators in Rust with examples.