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]
Article Tags :