String Slices क्या हैं
In this page:
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
// 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);
}
Login to try C/C++/Java/PHP code in the editor
String Literals Are Slices
Source code में सीधे लिखा गया एक string literal, जैसे "Rust", का type &str है और program की पूरी अवधि के लिए जीता है, क्योंकि यह सीधे compiled binary में embedded है।
उदाहरण: String Literals Are Slices
// 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);
}
Login to try C/C++/Java/PHP code in the editor
Accepting &str in Functions
किसी function parameter को String के बजाय &str लिखना इसे borrowed String data और string literals दोनों accept करने देता है, function को callers के लिए ज़्यादा flexible बनाते हुए।
उदाहरण: Accepting &str in Functions
// 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));
}
Login to try C/C++/Java/PHP code in the editor
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
// 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);
}
Login to try C/C++/Java/PHP code in the editor
- किसी
Stringको एक multi-byte UTF-8 character के बीच में slice करने की कोशिश करना, जो runtime पर panic करता है। Stringको हर जगह उपयोग करना जहां एक function को सिर्फ text पढ़ना है, जब&straccept करना owned strings और literals दोनों के लिए काम करता।- यह मान लेना कि
&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 पहले से ही एक'staticlifetime वाले&strslices हैं।- वे functions जिन्हें सिर्फ string data पढ़ना है उन्हें आमतौर पर अधिकतम flexibility के लिए
Stringके बजाय&straccept करना चाहिए।
Chapter Quiz — Complete all 6 topics to unlock
0/6 topics done
Complete these topics first: