Rust Slices
What is a slice?
A data type without ownership is called slice. Slice does not relate to the entire collection; rather, it refers to a contiguous memory allocation. Without copying, it enables effective and safe access to an array. Slice is constructed using an existing variable rather than by itself. A slice is made up of length, which may or may not be changeable. Slices only act as arrays.
String slice
A segment of the string is referred to as a string slice. Slice appears as:
let str=String::from("javaTpoint tutorial");
let javaTpoint=&str[0..10];
let tutorial=&str[11,18];
We want to grab a portion of the string, not the complete string. A range that starts at start and ends at end is called a [start..end] range. As a result, we may define a slice by putting the range in brackets, like this: [start..end], where “start” is the element’s beginning position and “end” is one more than the slice’s ending position. ‘..=’ must be used in place of ‘..’ if we wish to include the end of the string.
let str= String::from("javaTpoint tutorial");
let javaTpoint = &str[0..=9];
let tutorial= &str[11..=18] ;
Diagrammatically representation:
We can also remove the starting index if you would prefer to start the index at 0. It appears as follows:
let str= String::from("hello world");
let hello = &str[0..5];
let hello=&str[..5];
We can remove the initial index if slice contains the final byte of the text. It appears as follows:
let str= String::from("hello world") ;
let hello=&str[6..len];
let world = &str[6..];
Example
fn main()
let str=String::from("javaTpoint tutorial");
let javatpoint=&str[..=9];
println!("first word of the given string is {}",javatpoint);
Output:
first word of the given string is javaTpoint
String slices are literals
String literals are only thought of as string slices and are stored in binary. Let’s examine:
let str = "Hello javaTpoint" ;
‘&str’ is the type of’str’. It is a slice that points to a particular binary point. ‘&str’ is an immutable reference, and string literals are immutable as well.
String slices as parameters
We can pass a string slice directly if we have one. To make an API more broad and useful without losing its functionality, we send the string slice as an argument to the function instead of the reference.
fn main()
{
let str= String:: from("Computer Science");
let first_word= first_word(&str[..]); //first_word function finds the first word of the string.
let s="Computer Science" ; //string literal
let first_word=first_word(&s[..]); // first_word function finds the first word of the string.
let first_word=first_word(s) ; //string slice is same as string literal. Therefore, it can also be
written in this way also.
}
Other slices
Slices can also be applied to an array. They function similarly to a slice of string. The type of the slice is [&i32]. By holding a reference as the first element and length as the second, they function similarly to a string slice.
Consider a array:
let arr = [100,200,300,400,500]; // array initialization
et a = &arr[1..=3]; // retrieving second,third and fourth element
Example
fn main()
let arr = [100,200,300,400,500,600];
let mut i=0;
let a=&arr[1..=3];
let len=a.len();
println!("Elements of 'a' array:");
while i
Output:
Elements of 'a' array:
200
300
400