Rust - Slice

eslerkang·2022년 3월 8일
0

Rust

목록 보기
2/10
post-thumbnail

Slice

Slice is a reference of data collection. It can be a part of String, array..

As its dynamically sized type and a part of some fixed sized type, so we use reference to tell the compiler a size of it.

fn main() {
  let seasons = ["Spring", "Summer", "Fall", "Winter"];
  println!("{:?}", &seasons[..=4]);
}

We can make slice by using [0..4], and it is similar to python's range(0, 4).

As a python's range, it means we will start at 0 and end before 4. So we are not including 4th value.

But if you want to include 4th value at an example above, you can use [0..=4] '=' to mean I will include the end point.

Also we can omit the start or end value. And it will mean I will start at the start index of collection or end at the end index of collection.
Like [..4] is 0 1 2 3, [1..] is [1, 2, 3, ....to the end of collection]

As the seasons array is [&str], the type of slice is [&str] too.

profile
Surfer surfing on the dynamic flow of the world

0개의 댓글