{

Range Operator in Rust with example


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

The range operator is also called two periods(..) in Rust. Here is a syntax

start .. end

the start is a starting value the end is an end value.

A range of operators can be used in Array and vectors to get the Slice of an object.

Rust Range Operator

Rust provides multiple variations of range operators.

  • Range It includes the start value and excludes the end value. syntax
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 It includes the start value and the end value is omitted. syntax
1..

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

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 It is considered the end value and the start value is omitted.

Start value starts from 1 End value considered as end value -1. syntax

..5

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

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 It is considered a start and end value. The start value is considered zero. The end value is considered as end-1. syntax
..

The returned values are 0,1,2,3,4 etc values forever.

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

It includes equal operators such as start..=end It considered start and end value. The Start value is considered as zero. End value is considered as end. syntax

0..=4

The returned values are 0,1,2,3,4 etc values forever.

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

It includes equal operators such as ..=end and starts value is omitted. It is considered an end value. The start value is considered zero. The end value is considered as the end. syntax

..=4

The returned values are 0,1,2,3,4 etc values forever.

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

Learned multiple range operators in Rust with examples

THE BEST NEWSLETTER ANYWHERE
Join 6,000 subscribers and get a daily digest of full stack tutorials delivered to your inbox directly.No spam ever. Unsubscribe any time.

Similar Posts
Subscribe
You'll get a notification every time a post gets published here.