Slice is a data type that does not have ownership. Slice references a contiguous memory allocation rather than the whole collection. Slices are also present in Python which is similar to slice here in Rust. Slice is used when you do not want the complete collection, or you want some part of it.
Slicing Key Points :
- In slicing first element is at 0 index and the last is index-1.
//gfg is String or Array
&gfg[0..2] //from 0 to 1 index
&gfg[0..3] //from 0 to 2 index
- If you want to start from 0 then you do not need to pass 0.
//gfg is String or Array
&gfg[..2] //from 0 to 1 index
&gfg[..3] //from 0 to 3 index
- If you want to go to the end of the String or Array first calculate the length then pass it.
//gfg is String or Array
//calculate length using len() method
length_of_string=gfg.len();
//from 0 to last index
&gfg[..lenght_of_string]
//from 4th element or index=3 to last index
&gfg[3..length_of_string]
Approach Of Example 1 :
- For first character we will start from &gfg[0..1] (0,1).
- For first three characters we will use &gfg[..3] or &gfg[0..3].
- For starting from 0 to x &gfg[..x] or&gfg[0..x].
- For starting from 0 to length of string , for this we need to calculate length first using len() method than we can use &gfg[..x] or &gfg[0..length_of_string].
- For starting from x to length than &gfg[x..length_of_string].
Example 1: Slicing of String in Rust
Rust
// Rust program for slicing of String
fn main() {
// String
let gfg = "GFG is a great start to start coding and improve".to_string();
// for first character
println!("first character ={}",&gfg[0..1] );
// for first three characters
println!("first three character ={}",&gfg[..3] );
// calculating length of String
let length_of_string=gfg.len();
let x=5;
// start from first to last character
println!("start from 0 to x ={}",&gfg[..x] );
// start from x to last character
println!("start from x to end ={}",&gfg[x..length_of_string]);
// start from first to last character
println!("from start to last ={}",&gfg[..length_of_string])
}
Output :
first character =G
first three character =GFG
start from 0 to x =GFG i
start from x to end =s a great start to start coding and improve
from start to last =GFG is a great start to start coding and improve
Approach Of Example 2:
- For first value in array we will start from &gfg[0..1] (0,1).
- For first three values in array we will use &gfg[..3] or &gfg[0..3].
- For starting from 0 to x &gfg[..x] or &gfg[0..x].
- For starting from 0 to length of the Array, for this we need to calculate length first using len() method than we can use &gfg[..length_of_array] or &gfg[0..length_of_array].
- For starting from x to length of the array &gfg[x..length_of_array].
Example 2: Slicing the Array
Rust
// Rust program for slicing Array
fn main() {
let gfg = [10, 22, 31, 43, 57];
let length_of_array=gfg.len();
let x=2;
// for first value
println!("for first= {:?}",&gfg[0..1]);
// for first three value
println!("for first three= {:?}",&gfg[0..3]);
// for first to last(or length)
println!("for 0 to length of the array= {:?}",&gfg[0..length_of_array] );
println!("{:?}",&gfg[x..length_of_array]);
}
Output :
for first= [10]
for first three= [10, 22, 31]
for 0 to length of the array= [10, 22, 31, 43, 57]
[31, 43, 57]
Similar Reads
Rust - Strings String data type is a very important part of any programming language. Rust handles strings a bit differently from other languages. Â The String data type in Rust is of two types: String Literal (&str)String Object (String)String LiteralString Literal or &str are called 'string slices', whic
2 min read
Rust - Tuple A tuple in rust is a finite heterogeneous compound data type, meaning it can store more than one value at once. In tuples there is no inbuilt method to add elements into a tuple. We can use the index to get the value of a tuple, and we also can not iterate over a tuple using for loop. Tuples in Rust
2 min read
Rust - Traits A trait tells the Rust compiler about functionality a particular type has and can share with other types. Traits are an abstract definition of shared behavior amongst different types. So, we can say that traits are to Rust what interfaces are to Java or abstract classes are to C++. A trait method is
8 min read
Rust - Vectors Vector is a module in Rust that provides the container space to store values. It is a contiguous resizable array type, with heap-allocated contents. It is denoted by Vec<T>. Vectors in Rust have O(1) indexing and push and pop operations in vector also take  O(1) complexity. Vectors ensure they
4 min read
Rust - Supertraits In Rust, we have a concept of super traits. Rust being a modern systems-based programming language focuses mainly on the speed, safe and concurrent aspects of programming. Â Supertraits in Rust are traits that require implementation for conversion of specific types for implementing specific traits.
2 min read
Rust - Switch Case Switch statement is basically nested if statement, though it does not support expression, it matches an int, string, or boolean data type variable with a given set of cases that are provided by the programmer. It is mainly used in a menu-driven program where the user selects which function they want
2 min read