← Back to Rust Course | Chapter 2: Variables & Types | Lesson 7 of 7

Type Inference क्या है

Rust ज़्यादातर समय यह समझने के लिए काफी smart है कि आपका मतलब किस तरह की value से था, इसलिए आपको अक्सर इसे खुद कहने की ज़रूरत नहीं पड़ती।
Syntax
rust
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

markup
// 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);
}

Inference from Later Usage

Rust एक variable का type बाद में code में इसके उपयोग होने के तरीके से भी infer कर सकता है, सिर्फ इसकी initial value से नहीं, जब तक usage unambiguous हो।

Note: यहां Rust पहली push(1) call देखने के बाद ही Vec<i32> infer करता है।

उदाहरण: Inference from Later Usage

markup
// 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);
}

Function Signatures Need Explicit Types

Local variables के विपरीत, function parameters और return types कभी infer नहीं होते -- उन्हें हमेशा explicitly लिखा जाना चाहिए। यह function signatures को एक reliable, self-documenting contract बनाता है।

उदाहरण: Function Signatures Need Explicit Types

markup
// 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));
}

When Inference Fails

कभी-कभी compiler खुद से एक type determine नहीं कर सकता, जैसे जब एक generic parsing method कई अलग-अलग types produce कर सकता हो। उस case में आपको ambiguity resolve करने के लिए एक explicit annotation देना होगा।

उदाहरण: When Inference Fails

markup
// 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);
}
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. यह मानना कि Rust dynamically typed है क्योंकि आप शायद ही कभी type annotations लिखते हैं -- हर value का अब भी एक fixed type है, सिर्फ infer किया गया।
  2. एक case में type annotation छोड़ देना जहां compiler असल में infer नहीं कर सकता, एक compile error की ओर ले जाते हुए।
  3. यह मान लेना कि 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:

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.