Integer के Types
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
// 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);
}
Login to try C/C++/Java/PHP code in the editor
Default Integer Type
जब आप बिना explicit type के एक integer literal लिखते हैं और compiler context से एक infer नहीं कर सकता, Rust default रूप से i32 उपयोग करता है, जो modern hardware पर एक अच्छा general-purpose choice है।
उदाहरण: Default Integer Type
// 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);
}
Login to try C/C++/Java/PHP code in the editor
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
// 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]);
}
Login to try C/C++/Java/PHP code in the editor
Integer Overflow
हर integer type की एक fixed range है, और इसे exceed करना overflow कहलाता है। एक debug build में, Rust overflow पर तुरंत panic करता है ताकि bug जल्दी पकड़ा जाए; एक release build में यह इसके बजाय wrap around करता है, इसलिए overflow behavior पर भरोसा करना खतरनाक है।
checked_add panic या wrap करने के बजाय None return करता है, आपको overflow explicitly handle करने देते हुए।उदाहरण: Integer Overflow
// 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);
}
Login to try C/C++/Java/PHP code in the editor
- किसी value के लिए
i8जैसे छोटे integer type उपयोग करना जो इसकी range से बड़ा हो सकती है, debug mode में एक overflow panic का कारण बनते हुए। - किसी expression में signed और unsigned integer types (जैसे
i32औरu32) को बिना explicit conversion के mix करना, जो compile होने में fail होता है। - यह मान लेना कि integers default रूप से
i64हैं -- Rust असल मेंi32default करता है जब कोई type अन्यथा infer न हो सके।
- Integer types को signedness और size से नाम दिया जाता है, जैसे
i32(signed 32-bit) औरu8(unsigned 8-bit)। isize/usizepointer-sized integers हैं, आमतौर पर collections indexing के लिए उपयोग होते हैं।- Rust एक unannotated integer literal को
i32default करता है जब तक 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: