Float, Boolean और Char
let name: f64 = 1.5;
let name: bool = true;
let name: char = 'a';
Floating-Point Numbers
Rust में दो floating-point types हैं: f32 (single precision) और f64 (double precision, और default)। ये fractional parts वाले numbers represent करते हैं, IEEE 754 standard follow करते हुए।
उदाहरण: Floating-Point Numbers
// Entry point: execution of the program starts here
fn main() {
// Declare an immutable variable `price` (a floating-point number), set to `19.99`
let price: f64 = 19.99;
// Declare an immutable variable `discount` (a 32-bit float), set to `0.15`
let discount: f32 = 0.15;
// Print "price: {}, discount: {}" to the console, substituting in `price, discount` (with a trailing newline)
println!("price: {}, discount: {}", price, discount);
}
Login to try C/C++/Java/PHP code in the editor
Boolean Values
bool type में बिल्कुल दो possible values हैं, true और false, और यह एक byte size का है। Booleans ज़्यादातर comparison operators से produce होते हैं और if expressions से consume होते हैं।
उदाहरण: Boolean Values
// Entry point: execution of the program starts here
fn main() {
// Declare an immutable variable `is_learning`, set to `true`
let is_learning = true;
// Declare an immutable variable `is_finished`, set to `false`
let is_finished = false;
// Print "learning: {}, finished: {}" to the console, substituting in `is_learning, is_finished` (with a trailing newline)
println!("learning: {}, finished: {}", is_learning, is_finished);
}
Login to try C/C++/Java/PHP code in the editor
Character Type
char type एक single Unicode scalar value रखता है, single quotes में लिखा गया। चूंकि Rust का char चार bytes का है, यह plain ASCII letters से कहीं ज़्यादा represent कर सकता है, emoji और कई languages के characters सहित।
उदाहरण: Character Type
// Entry point: execution of the program starts here
fn main() {
// Declare an immutable variable `letter`, set to `'R'`
let letter = 'R';
// Declare an immutable variable `emoji`, set to `'🦀'`
let emoji = '🦀';
// Print "letter: {}, emoji: {}" to the console, substituting in `letter, emoji` (with a trailing newline)
println!("letter: {}, emoji: {}", letter, emoji);
}
Login to try C/C++/Java/PHP code in the editor
Comparing Floats Safely
चूंकि floating-point math tiny rounding errors introduce कर सकता है, दो floats को == से compare करना जोखिम भरा है। एक safer approach यह जांचता है कि उनके बीच का difference एक छोटी tolerance value से कम है।
उदाहरण: Comparing Floats Safely
// Entry point: execution of the program starts here
fn main() {
// Declare an immutable variable `a` (a floating-point number), set to `0.1 + 0.2`
let a: f64 = 0.1 + 0.2;
// Declare an immutable variable `b` (a floating-point number), set to `0.3`
let b: f64 = 0.3;
// Declare an immutable variable `close_enough`, set to `(a - b).abs() < 1e-10`
let close_enough = (a - b).abs() < 1e-10;
// Print "a is close enough to b: {}" to the console, substituting in `close_enough` (with a trailing newline)
println!("a is close enough to b: {}", close_enough);
}
Login to try C/C++/Java/PHP code in the editor
- exact equality के लिए floating-point numbers को
==से compare करना, जब tiny rounding errors उन्हें unequal बना सकती हैं। - यह मान लेना कि Rust में
charC की तरह एक byte है -- एक Rustcharअसल में एक 4-byte Unicode scalar value है। - यह भूल जाना कि Rust में boolean values सख्ती से
true/falseहैं, कुछ दूसरी languages की तरह0/1integers नहीं।
f32औरf64floating-point numbers represent करते हैं;f64default है और इसमें ज़्यादा precision है।boolvalues बिल्कुलtrueयाfalseहैं और integers के साथ interchangeable नहीं हैं।charएक single Unicode scalar value represent करता है, single quotes से लिखा गया, जैसेaया'🦀'।- ये सभी Rust के basic scalar types हैं, integers के साथ।
Chapter Quiz — Complete all 7 topics to unlock
0/7 topics done
Complete these topics first: