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

References के Basics

एक reference आपको किसी और की value देखने और उपयोग करने देता है, बिना इसे खुद own किए।
Syntax
rust
let r = &value;
fn name(param: &Type) {
    // borrows without owning
}
name(&value);

Creating a Reference

किसी variable के पहले & prefix करना इसकी value को move या copy करने के बजाय इसका एक reference बनाता है। Reference receive करने वाला function इसे own किए बिना data पढ़ सकता है।

उदाहरण: Creating a Reference

markup
// Define the function `print_value` taking `v`
fn print_value(v: &i32) {
    // Print "Value is {}" to the console, substituting in `v` (with a trailing newline)
    println!("Value is {}", v);
}

// Entry point: execution of the program starts here
fn main() {
    // Declare an immutable variable `number`, set to `42`
    let number = 42;
    print_value(&number);
    // Print "Still usable: {}" to the console, substituting in `number` (with a trailing newline)
    println!("Still usable: {}", number);
}

References to Strings

References String जैसे बड़े data के लिए विशेष रूप से उपयोगी हैं, एक function को caller से ownership लिए बिना content पढ़ने देते हुए।

उदाहरण: References to Strings

markup
// Define the function `print_length` taking `s`
fn print_length(s: &String) {
    // Print "Length: {}" to the console, substituting in `s.len()` (with a trailing newline)
    println!("Length: {}", s.len());
}

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

References Are Immutable by Default

एक plain reference &value सिर्फ इसके point किए गए data को पढ़ने देता है, लिखने नहीं। यह let bindings की default immutability से मेल खाता है।

उदाहरण: References Are Immutable by Default

markup
// Entry point: execution of the program starts here
fn main() {
    // Declare an immutable variable `data`, set to `vec![1, 2, 3]`
    let data = vec![1, 2, 3];
    // Declare an immutable variable `reference`, set to `&data`
    let reference = &data;
    // Print "Through reference: {:?}" to the console, substituting in `reference` (with a trailing newline)
    println!("Through reference: {:?}", reference);
}

The Original Owner Remains Valid

चूंकि & से borrowing ownership transfer नहीं करती, original variable borrow के दौरान और उसके बाद दोनों में पूरी तरह usable रहता है, एक move के विपरीत।

उदाहरण: The Original Owner Remains Valid

markup
// Define the function `describe` taking `s`
fn describe(s: &String) {
    // Print "Describing: {}" to the console, substituting in `s` (with a trailing newline)
    println!("Describing: {}", s);
}

// Entry point: execution of the program starts here
fn main() {
    // Declare an immutable variable `word`, set to `String::from("borrow")`
    let word = String::from("borrow");
    describe(&word);
    describe(&word);
    // Print "word used three times total: {}" to the console, substituting in `word` (with a trailing newline)
    println!("word used three times total: {}", word);
}
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. किसी function को call करते समय & भूल जाना जो एक reference expect करता है, एक type mismatch error का कारण बनते हुए।
  2. यह मान लेना कि एक reference data की एक copy है -- यह सिर्फ original value का एक pointer है, कोई नई value नहीं।
  3. एक immutable reference के ज़रिए data modify करने की कोशिश करना, जिसे compiler reject करता है।
चैप्टर सारांश
  • एक reference, &value लिखा गया, आपको किसी value को access करने देता है बिना इसकी ownership लिए।
  • References default रूप से immutable हैं, बिल्कुल let bindings की तरह।
  • किसी value को value से pass करने के बजाय इसे reference करना borrowing कहलाता है।
  • Referenced value का owner unaffected रहता है और borrow खत्म होने के बाद भी उपयोग किया जा सकता है।
🔒

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.