Ownership और Functions
In this page:
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
// 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);
}
Login to try C/C++/Java/PHP code in the editor
Returning Ownership
एक function किसी value की ownership सिर्फ इसे final expression के रूप में return करके अपने caller को वापस transfer कर सकता है, caller को एक नई binding के तहत data उपयोग करते रहने देते हुए।
उदाहरण: Returning Ownership
// 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);
}
Login to try C/C++/Java/PHP code in the editor
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
// 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);
}
Login to try C/C++/Java/PHP code in the editor
Avoiding Unnecessary Moves with References
हर बार data पढ़ने की ज़रूरत पर ownership move करने के बजाय, एक function एक reference accept कर सकता है (अगले chapter में covered), caller को call के बाद अपने original variable उपयोग करते रहने देते हुए।
उदाहरण: Avoiding Unnecessary Moves with References
// 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);
}
Login to try C/C++/Java/PHP code in the editor
- किसी
Stringको value से किसी function में pass करना और फिर बाद में caller में इसे फिर उपयोग करने की कोशिश करना। - यह न समझना कि किसी function से एक value return करना ownership वापस caller को transfer करता है।
- उन functions में owned parameters (
Stringबजाय&strके) overuse करना जिन्हें सिर्फ temporarily data पढ़ना है।
- किसी non-
Copyvalue को 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: