← Back to Rust Course | Chapter 6: Borrowing & References | Lesson 5 of 6

Slices क्या हैं

एक slice एक window जैसा है जो आपको एक बड़ी list का सिर्फ एक हिस्सा दिखाता है, इसमें से कुछ भी copy किए बिना।
Syntax
rust
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

markup
// 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);
}

Slicing a Vector

Slices एक Vec पर भी वैसे ही काम करते हैं, आपको इसके एक हिस्से को &[T] के रूप में देखने देते हुए जबकि Vec खुद data की ownership रखता है।

उदाहरण: Slicing a Vector

markup
// 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);
}

Full Slices

किसी range के दोनों ends छोड़ना, &collection[..], पूरे collection को cover करने वाला एक slice produce करता है, जो तब उपयोगी है जब एक function parameter एक owned collection के बजाय एक slice type expect करता है।

उदाहरण: Full Slices

markup
// 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[..]));
}

Slices Borrow Their Source

चूंकि एक slice एक reference है, यह वही borrowing rules follow करता है: original collection तब तक mutate नहीं हो सकता जब तक इसका एक active slice अब भी मौजूद है।

उदाहरण: Slices Borrow Their Source

markup
// 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);
}
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
आम गलतियां
  1. bounds से बाहर slice करने की कोशिश करना, जैसे एक 5-element array पर &array[0..10], जो runtime पर panic करता है।
  2. यह भूल जाना कि slices original data से borrow करते हैं, इसलिए original collection तब तक mutate नहीं हो सकता जब तक इसका एक slice उपयोग में है।
  3. 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:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.