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

String Slices क्या हैं

एक string slice कुछ text के हिस्से की एक झलक है, copy नहीं borrowed, इसलिए यह तेज़ है और memory waste नहीं करता।
Syntax
rust
let s = String::from("text");
let part: &str = &s[start..end];
let part = &s[..end];

Creating a String Slice

एक string slice एक byte-range index उपयोग करके किसी String के data के हिस्से को reference करता है। यह text को own या copy नहीं करता -- यह बस original string की memory में point करता है।

उदाहरण: Creating a String Slice

markup
// Entry point: execution of the program starts here
fn main() {
    // Declare an immutable variable `greeting`, set to `String::from("Hello, world!")`
    let greeting = String::from("Hello, world!");
    // Declare an immutable variable `hello`, set to `&greeting[0..5]`
    let hello = &greeting[0..5];
    // Print "{}" to the console, substituting in `hello` (with a trailing newline)
    println!("{}", hello);
}

String Literals Are Slices

Source code में सीधे लिखा गया एक string literal, जैसे "Rust", का type &str है और program की पूरी अवधि के लिए जीता है, क्योंकि यह सीधे compiled binary में embedded है।

उदाहरण: String Literals Are Slices

markup
// Entry point: execution of the program starts here
fn main() {
    // Declare an immutable variable `literal` (a string slice), set to `"I am already a slice"`
    let literal: &str = "I am already a slice";
    // Print "{}" to the console, substituting in `literal` (with a trailing newline)
    println!("{}", literal);
}

Accepting &str in Functions

किसी function parameter को String के बजाय &str लिखना इसे borrowed String data और string literals दोनों accept करने देता है, function को callers के लिए ज़्यादा flexible बनाते हुए।

उदाहरण: Accepting &str in Functions

markup
// Define the function `first_word` taking `text` and returning a string slice
fn first_word(text: &str) -> &str {
    // Match on `text.find(' ')` and branch by pattern
    match text.find(' ') {
        Some(index) => &text[..index],
        None => text,
    }
}

// Entry point: execution of the program starts here
fn main() {
    // Declare an immutable variable `sentence`, set to `String::from("Rust is fun")`
    let sentence = String::from("Rust is fun");
    // Print "{}" to the console, substituting in `first_word(&sentence)` (with a trailing newline)
    println!("{}", first_word(&sentence));
}

Slicing on Character Boundaries

चूंकि Rust strings UTF-8 encoded हैं, slice indices valid character boundaries पर होने चाहिए।

किसी multi-byte character के बीच में slice करना एक runtime panic का कारण बनता है, इसलिए text-safe code अक्सर raw byte indices के बजाय .chars() जैसे methods उपयोग करता है।

उदाहरण: Slicing on Character Boundaries

markup
// Entry point: execution of the program starts here
fn main() {
    // Declare an immutable variable `word`, set to `"Rust"`
    let word = "Rust";
    // Declare an immutable variable `piece`, set to `&word[0..2]`
    let piece = &word[0..2];
    // Print "First two bytes: {}" to the console, substituting in `piece` (with a trailing newline)
    println!("First two bytes: {}", piece);
}
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. किसी String को एक multi-byte UTF-8 character के बीच में slice करने की कोशिश करना, जो runtime पर panic करता है।
  2. String को हर जगह उपयोग करना जहां एक function को सिर्फ text पढ़ना है, जब &str accept करना owned strings और literals दोनों के लिए काम करता।
  3. यह मान लेना कि &str और String एक ही type हैं -- &str एक borrowed view है जबकि String एक owned, growable buffer है।
चैप्टर सारांश
  • &str कुछ UTF-8 text का एक immutable reference है, या तो एक String से borrowed या एक string literal।
  • String slices byte-index ranges उपयोग करते हैं, जैसे &text[0..5], और इन्हें valid UTF-8 character boundaries पर आना चाहिए।
  • "hello" जैसे String literals पहले से ही एक 'static lifetime वाले &str slices हैं।
  • वे functions जिन्हें सिर्फ string data पढ़ना है उन्हें आमतौर पर अधिकतम flexibility के लिए String के बजाय &str 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.