Slices क्या हैं
let slice = &array[start..end];
let slice = &vec[..end];
let slice = &vec[start..];
fn name(param: &[Type]) {}
Slicing an Array
एक slice square brackets के अंदर range syntax उपयोग करके किसी collection के एक contiguous हिस्से को reference करता है, underlying elements copy किए बिना।
उदाहरण: Slicing an Array
// Entry point: execution of the program starts here
fn main() {
// Declare an immutable variable `numbers`, set to `[10, 20, 30, 40, 50]`
let numbers = [10, 20, 30, 40, 50];
// Declare an immutable variable `middle`, set to `&numbers[1..4]`
let middle = &numbers[1..4];
// Print "{:?}" to the console, substituting in `middle` (with a trailing newline)
println!("{:?}", middle);
}
Login to try C/C++/Java/PHP code in the editor
Slicing a Vector
Slices एक Vec पर भी वैसे ही काम करते हैं, आपको इसके एक हिस्से को &[T] के रूप में देखने देते हुए जबकि Vec खुद data की ownership रखता है।
उदाहरण: Slicing a Vector
// Entry point: execution of the program starts here
fn main() {
// Declare an immutable variable `scores`, set to `vec![90, 85, 77, 92]`
let scores = vec![90, 85, 77, 92];
// Declare an immutable variable `top_two`, set to `&scores[0..2]`
let top_two = &scores[0..2];
// Print "Top two: {:?}" to the console, substituting in `top_two` (with a trailing newline)
println!("Top two: {:?}", top_two);
}
Login to try C/C++/Java/PHP code in the editor
Full Slices
किसी range के दोनों ends छोड़ना, &collection[..], पूरे collection को cover करने वाला एक slice produce करता है, जो तब उपयोगी है जब एक function parameter एक owned collection के बजाय एक slice type expect करता है।
उदाहरण: Full Slices
// Define the function `total` taking `values` and returning an integer
fn total(values: &[i32]) -> i32 {
values.iter().sum()
}
// Entry point: execution of the program starts here
fn main() {
// Declare an immutable variable `data`, set to `vec![1, 2, 3, 4]`
let data = vec![1, 2, 3, 4];
// Print "Sum: {}" to the console, substituting in `total(&data[..])` (with a trailing newline)
println!("Sum: {}", total(&data[..]));
}
Login to try C/C++/Java/PHP code in the editor
Slices Borrow Their Source
चूंकि एक slice एक reference है, यह वही borrowing rules follow करता है: original collection तब तक mutate नहीं हो सकता जब तक इसका एक active slice अब भी मौजूद है।
उदाहरण: Slices Borrow Their Source
// Entry point: execution of the program starts here
fn main() {
// Declare an immutable variable `letters`, set to `['a', 'b', 'c', 'd']`
let letters = ['a', 'b', 'c', 'd'];
// Declare an immutable variable `part`, set to `&letters[1..3]`
let part = &letters[1..3];
// Print "Slice: {:?}" to the console, substituting in `part` (with a trailing newline)
println!("Slice: {:?}", part);
// Print "Original still usable: {:?}" to the console, substituting in `letters` (with a trailing newline)
println!("Original still usable: {:?}", letters);
}
Login to try C/C++/Java/PHP code in the editor
- bounds से बाहर slice करने की कोशिश करना, जैसे एक 5-element array पर
&array[0..10], जो runtime पर panic करता है। - यह भूल जाना कि slices original data से borrow करते हैं, इसलिए original collection तब तक mutate नहीं हो सकता जब तक इसका एक slice उपयोग में है।
- functions को data pass करते समय
&vec[..](पूरे vector का एक slice) कोvecखुद (एक owned value) के साथ confuse करना।
- एक slice,
&[T]या&strलिखा गया, elements की एक contiguous sequence को बिना owned किए reference करता है। 1..3जैसा range syntax slice करने के लिए किसी collection की एक sub-range select करता है।- Slices अपने source से borrow करते हैं, इसलिए borrowing rules अब भी उन पर लागू होते हैं।
- Slices functions को एक flexible parameter type के साथ पूरे collections और partial views दोनों accept करने देते हैं।
Chapter Quiz — Complete all 6 topics to unlock
0/6 topics done
Complete these topics first: