← Back to Rust Course | Chapter 5: Ownership | Lesson 6 of 6

Ownership और Functions

जब आप किसी function को एक value देते हैं, तो आप अक्सर इसे हमेशा के लिए दे रहे होते हैं, जब तक आप इसे वापस न मांगें।
Syntax
rust
fn take(param: Type) {
    // takes ownership
}

fn give() -> Type {
    // returns ownership
}

Passing Ownership Into a Function

जब एक non-Copy value जैसे String को किसी function में value से pass किया जाता है, ownership उस function के parameter में move हो जाती है, और caller बाद में original variable उपयोग नहीं कर सकता।

उदाहरण: Passing Ownership Into a Function

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

// Entry point: execution of the program starts here
fn main() {
    // Declare an immutable variable `text`, set to `String::from("gift")`
    let text = String::from("gift");
    consume(text);
}

Returning Ownership

एक function किसी value की ownership सिर्फ इसे final expression के रूप में return करके अपने caller को वापस transfer कर सकता है, caller को एक नई binding के तहत data उपयोग करते रहने देते हुए।

उदाहरण: Returning Ownership

markup
// Define the function `create_greeting` with no parameters and returning a String
fn create_greeting() -> String {
    String::from("Hello from a function")
}

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

Passing Ownership In and Returning It Back Out

एक function किसी value की ownership accept कर सकता है, इसे उपयोग कर सकता है, और फिर इसे वापस return कर सकता है, references के बजाय moves के ज़रिए call की अवधि के लिए इसे effectively "borrow" करते हुए।

उदाहरण: Passing Ownership In and Returning It Back Out

markup
// Define the function `add_exclamation` taking `s` and returning a String
fn add_exclamation(mut s: String) -> String {
    // Append `'!'` to `s`
    s.push('!');
    s
}

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

Avoiding Unnecessary Moves with References

हर बार data पढ़ने की ज़रूरत पर ownership move करने के बजाय, एक function एक reference accept कर सकता है (अगले chapter में covered), caller को call के बाद अपने original variable उपयोग करते रहने देते हुए।

उदाहरण: Avoiding Unnecessary Moves with References

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

// Entry point: execution of the program starts here
fn main() {
    // Declare an immutable variable `word`, set to `String::from("borrowed")`
    let word = String::from("borrowed");
    print_length(&word);
    // Print "Still usable: {}" to the console, substituting in `word` (with a trailing newline)
    println!("Still usable: {}", 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. किसी String को value से किसी function में pass करना और फिर बाद में caller में इसे फिर उपयोग करने की कोशिश करना।
  2. यह न समझना कि किसी function से एक value return करना ownership वापस caller को transfer करता है।
  3. उन functions में owned parameters (String बजाय &str के) overuse करना जिन्हें सिर्फ temporarily data पढ़ना है।
चैप्टर सारांश
  • किसी non-Copy value को value से किसी function में pass करना ownership को उस function में move करता है।
  • एक function किसी value की ownership return कर सकता है, इसे वापस caller को transfer करते हुए।
  • Tuples का उपयोग कई values return करने के लिए किया जा सकता है, एक computed result के साथ एक value वापस देना सहित।
  • एक owned value के बजाय एक reference accept करना किसी function को बिना ownership लिए temporarily data उपयोग करने देता है।
🔒

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.