Type Inference क्या है
In this page:
let name = value; // type inferred
let name: Type = expr.parse().unwrap();
Inference from Literals
जब आप let x = 5; लिखते हैं, Rust literal को देखता है और बिना आपके type लिखे इसे i32 default करता है। Compiler compile time पर यह inference perform करता है -- इसमें कोई runtime type-checking शामिल नहीं है।
उदाहरण: Inference from Literals
// Entry point: execution of the program starts here
fn main() {
// Declare an immutable variable `count`, set to `5`
let count = 5;
// Declare an immutable variable `name`, set to `"Rustacean"`
let name = "Rustacean";
// Print "{} appears {} times" to the console, substituting in `name, count` (with a trailing newline)
println!("{} appears {} times", name, count);
}
Login to try C/C++/Java/PHP code in the editor
Inference from Later Usage
Rust एक variable का type बाद में code में इसके उपयोग होने के तरीके से भी infer कर सकता है, सिर्फ इसकी initial value से नहीं, जब तक usage unambiguous हो।
push(1) call देखने के बाद ही Vec<i32> infer करता है।उदाहरण: Inference from Later Usage
// Entry point: execution of the program starts here
fn main() {
// Declare a mutable variable `numbers`, initialized to `Vec::new()`
let mut numbers = Vec::new();
// Append `1` to `numbers`
numbers.push(1);
// Append `2` to `numbers`
numbers.push(2);
// Print "{:?}" to the console, substituting in `numbers` (with a trailing newline)
println!("{:?}", numbers);
}
Login to try C/C++/Java/PHP code in the editor
Function Signatures Need Explicit Types
Local variables के विपरीत, function parameters और return types कभी infer नहीं होते -- उन्हें हमेशा explicitly लिखा जाना चाहिए। यह function signatures को एक reliable, self-documenting contract बनाता है।
उदाहरण: Function Signatures Need Explicit Types
// Define the function `double` taking `n` and returning an integer
fn double(n: i32) -> i32 {
n * 2
}
// Entry point: execution of the program starts here
fn main() {
// Print "Doubled: {}" to the console, substituting in `double(21)` (with a trailing newline)
println!("Doubled: {}", double(21));
}
Login to try C/C++/Java/PHP code in the editor
When Inference Fails
कभी-कभी compiler खुद से एक type determine नहीं कर सकता, जैसे जब एक generic parsing method कई अलग-अलग types produce कर सकता हो। उस case में आपको ambiguity resolve करने के लिए एक explicit annotation देना होगा।
उदाहरण: When Inference Fails
// Entry point: execution of the program starts here
fn main() {
// Declare an immutable variable `parsed` (an integer), set to `"42".parse().unwrap()`
let parsed: i32 = "42".parse().unwrap();
// Print "Parsed as i32: {}" to the console, substituting in `parsed` (with a trailing newline)
println!("Parsed as i32: {}", parsed);
}
Login to try C/C++/Java/PHP code in the editor
- यह मानना कि Rust dynamically typed है क्योंकि आप शायद ही कभी type annotations लिखते हैं -- हर value का अब भी एक fixed type है, सिर्फ infer किया गया।
- एक case में type annotation छोड़ देना जहां compiler असल में infer नहीं कर सकता, एक compile error की ओर ले जाते हुए।
- यह मान लेना कि inference function boundaries में काम करता है -- function parameters और return types को हमेशा explicitly annotate किया जाना चाहिए।
- Rust context से types infer करता है, जैसे literal values, later usage, या return values।
- हर value का compile time पर अब भी बिल्कुल एक concrete type है -- inference Rust को dynamically typed नहीं बनाता।
- Function signatures को हमेशा explicit parameter और return types चाहिए, भले ही local variables अक्सर नहीं चाहते।
- जब inference ambiguous हो, compiler एक explicit annotation मांगते हुए एक error report करता है।
Chapter Quiz — Complete all 7 topics to unlock
0/7 topics done
Complete these topics first: