← Back to Rust Course | Chapter 9: Collections | Lesson 2 of 6

String और str में अंतर

String आपका अपना growable notebook है text का, जबकि &str किसी और के owned text की सिर्फ एक quick झलक है।
Syntax
rust
let s: &str = "text";
let s: String = String::from("text");
let s: String = "text".to_string();
let r: &str = &s;

Creating a String

String::from() या .to_string() heap पर एक owned, growable string बनाता है जिसे आप modify कर सकते हैं और जो scope से बाहर जाने पर अपने आप साफ़ हो जाएगा।

उदाहरण: Creating a String

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

Growing a String

चूंकि String अपना data own करता है, यह .push_str() जैसे methods से ज़्यादा text जोड़कर बढ़ सकता है, कुछ ऐसा जो एक plain &str नहीं कर सकता।

उदाहरण: Growing a String

markup
// Entry point: execution of the program starts here
fn main() {
    // Declare a mutable variable `message`, initialized to `String::from("Hello")`
    let mut message = String::from("Hello");
    message.push_str(", Rust!");
    // Print "{}" to the console, substituting in `message` (with a trailing newline)
    println!("{}", message);
}

Converting Between String and &str

आप एक String को &owned (या .as_str()) से एक &str के रूप में borrow कर सकते हैं, और एक &str को .to_string() या String::from() से एक owned String में convert कर सकते हैं।

उदाहरण: Converting Between String and &str

markup
// Entry point: execution of the program starts here
fn main() {
    // Declare an immutable variable `owned` (a String), set to `String::from("borrowed view")`
    let owned: String = String::from("borrowed view");
    // Declare an immutable variable `borrowed` (a string slice), set to `&owned`
    let borrowed: &str = &owned;
    // Print "{}" to the console, substituting in `borrowed` (with a trailing newline)
    println!("{}", borrowed);
    // Declare an immutable variable `back_to_owned` (a String), set to `borrowed.to_string()`
    let back_to_owned: String = borrowed.to_string();
    // Print "{}" to the console, substituting in `back_to_owned` (with a trailing newline)
    println!("{}", back_to_owned);
}

Choosing Function Parameter Types

किसी function में &str accept करना इसे string literals और borrowed String values दोनों के साथ काम करने देता है, इसे एक owned String parameter मांगने से ज़्यादा flexible बनाते हुए।

उदाहरण: Choosing Function Parameter Types

markup
// Define the function `shout` taking `text` and returning a String
fn shout(text: &str) -> String {
    format!("{}!!!", text.to_uppercase())
}

// Entry point: execution of the program starts here
fn main() {
    // Declare an immutable variable `owned`, set to `String::from("hello")`
    let owned = String::from("hello");
    // Print "{}" to the console, substituting in `shout(&owned)` (with a trailing newline)
    println!("{}", shout(&owned));
    // Print "{}" to the console, substituting in `shout("literal")` (with a trailing newline)
    println!("{}", shout("literal"));
}
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. किसी &str को सीधे बढ़ाने के लिए += उपयोग करने की कोशिश करना -- सिर्फ एक owned, mutable String में append किया जा सकता है।
  2. यह भूल जाना कि String::from() heap पर allocate करता है, जबकि एक &str literal सीधे binary में embedded है।
  3. &str के ज़्यादा flexibly owned strings और literals दोनों accept करने के बावजूद function parameter type के रूप में String उपयोग करना।
चैप्टर सारांश
  • String एक owned, growable, heap-allocated text buffer है।
  • &str string data में एक borrowed, immutable view है, या तो एक String से या एक literal से।
  • .to_string() या String::from() एक &str को एक owned String में convert करता है।
  • उन function parameters के लिए &str पसंद करें जिन्हें सिर्फ text पढ़ना है, और String जब आपको text own या grow करना हो।
🔒

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.