String और str में अंतर
String आपका अपना growable notebook है text का, जबकि &str किसी और के owned text की सिर्फ एक quick झलक है।In this page:
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
// 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);
}
Login to try C/C++/Java/PHP code in the editor
Growing a String
चूंकि String अपना data own करता है, यह .push_str() जैसे methods से ज़्यादा text जोड़कर बढ़ सकता है, कुछ ऐसा जो एक plain &str नहीं कर सकता।
उदाहरण: Growing a String
// 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);
}
Login to try C/C++/Java/PHP code in the editor
Converting Between String and &str
आप एक String को &owned (या .as_str()) से एक &str के रूप में borrow कर सकते हैं, और एक &str को .to_string() या String::from() से एक owned String में convert कर सकते हैं।
उदाहरण: Converting Between String and &str
// 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);
}
Login to try C/C++/Java/PHP code in the editor
Choosing Function Parameter Types
किसी function में &str accept करना इसे string literals और borrowed String values दोनों के साथ काम करने देता है, इसे एक owned String parameter मांगने से ज़्यादा flexible बनाते हुए।
उदाहरण: Choosing Function Parameter Types
// 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"));
}
Login to try C/C++/Java/PHP code in the editor
- किसी
&strको सीधे बढ़ाने के लिए+=उपयोग करने की कोशिश करना -- सिर्फ एक owned, mutableStringमें append किया जा सकता है। - यह भूल जाना कि
String::from()heap पर allocate करता है, जबकि एक&strliteral सीधे binary में embedded है। &strके ज़्यादा flexibly owned strings और literals दोनों accept करने के बावजूद function parameter type के रूप मेंStringउपयोग करना।
Stringएक owned, growable, heap-allocated text buffer है।&strstring data में एक borrowed, immutable view है, या तो एकStringसे या एक literal से।.to_string()याString::from()एक&strको एक ownedStringमें 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: