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

Integer के Types

Rust में बहुत सारे अलग-अलग sizes के whole numbers हैं ताकि आपका program सिर्फ उतनी ही memory उपयोग करे जितनी इसे असल में चाहिए।
Syntax
rust
let name: i32 = value;  // i8..i128, u8..u128, isize, usize
let name = 1_000u64;

Signed vs Unsigned

Signed integer types (i8, i16, i32, i64, i128) negative और positive दोनों numbers represent कर सकते हैं, जबकि unsigned types (u8, u16, u32, u64, u128) सिर्फ zero और positive numbers represent करते हैं, उन्हें उसी bit size के लिए double positive range देते हुए।

उदाहरण: Signed vs Unsigned

markup
// Entry point: execution of the program starts here
fn main() {
    // Declare an immutable variable `signed` (an integer), set to `-10`
    let signed: i32 = -10;
    // Declare an immutable variable `unsigned` (an unsigned integer), set to `10`
    let unsigned: u32 = 10;
    // Print "signed: {}, unsigned: {}" to the console, substituting in `signed, unsigned` (with a trailing newline)
    println!("signed: {}, unsigned: {}", signed, unsigned);
}

Default Integer Type

जब आप बिना explicit type के एक integer literal लिखते हैं और compiler context से एक infer नहीं कर सकता, Rust default रूप से i32 उपयोग करता है, जो modern hardware पर एक अच्छा general-purpose choice है।

उदाहरण: Default Integer Type

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

usize for Indexing

usize एक unsigned integer है जिसका size machine की pointer width से match करता है (आमतौर पर 64-bit)। यह वह type है जो Rust array/vector indices और lengths के लिए उपयोग करता है, क्योंकि किसी collection का size कभी negative नहीं हो सकता।

उदाहरण: usize for Indexing

markup
// Entry point: execution of the program starts here
fn main() {
    // Declare an immutable variable `numbers`, set to `[10, 20, 30]`
    let numbers = [10, 20, 30];
    // Declare an immutable variable `index` (an unsigned size integer), set to `1`
    let index: usize = 1;
    // Print "Element at index {}: {}" to the console, substituting in `index, numbers[index]` (with a trailing newline)
    println!("Element at index {}: {}", index, numbers[index]);
}

Integer Overflow

हर integer type की एक fixed range है, और इसे exceed करना overflow कहलाता है। एक debug build में, Rust overflow पर तुरंत panic करता है ताकि bug जल्दी पकड़ा जाए; एक release build में यह इसके बजाय wrap around करता है, इसलिए overflow behavior पर भरोसा करना खतरनाक है।

Note: checked_add panic या wrap करने के बजाय None return करता है, आपको overflow explicitly handle करने देते हुए।

उदाहरण: Integer Overflow

markup
// Entry point: execution of the program starts here
fn main() {
    // Declare an immutable variable `small` (an unsigned 8-bit integer), set to `250`
    let small: u8 = 250;
    // Declare an immutable variable `safe_sum`, set to `small.checked_add(3)`
    let safe_sum = small.checked_add(3);
    // Print "checked_add result: {:?}" to the console, substituting in `safe_sum` (with a trailing newline)
    println!("checked_add result: {:?}", safe_sum);
}
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. किसी value के लिए i8 जैसे छोटे integer type उपयोग करना जो इसकी range से बड़ा हो सकती है, debug mode में एक overflow panic का कारण बनते हुए।
  2. किसी expression में signed और unsigned integer types (जैसे i32 और u32) को बिना explicit conversion के mix करना, जो compile होने में fail होता है।
  3. यह मान लेना कि integers default रूप से i64 हैं -- Rust असल में i32 default करता है जब कोई type अन्यथा infer न हो सके।
चैप्टर सारांश
  • Integer types को signedness और size से नाम दिया जाता है, जैसे i32 (signed 32-bit) और u8 (unsigned 8-bit)।
  • isize/usize pointer-sized integers हैं, आमतौर पर collections indexing के लिए उपयोग होते हैं।
  • Rust एक unannotated integer literal को i32 default करता है जब तक context को अन्यथा ज़रूरत न हो।
  • Arithmetic overflow debug builds में panic करता है और release builds में चुपचाप wrap करता है जब तक explicitly handle न किया जाए।
🔒

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.