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

Float, Boolean और Char

Whole numbers के अलावा, Rust decimals वाले numbers, साधारण true/false answers, और single letters या symbols भी store कर सकता है।
Syntax
rust
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

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

Boolean Values

bool type में बिल्कुल दो possible values हैं, true और false, और यह एक byte size का है। Booleans ज़्यादातर comparison operators से produce होते हैं और if expressions से consume होते हैं।

उदाहरण: Boolean Values

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

Character Type

char type एक single Unicode scalar value रखता है, single quotes में लिखा गया। चूंकि Rust का char चार bytes का है, यह plain ASCII letters से कहीं ज़्यादा represent कर सकता है, emoji और कई languages के characters सहित।

उदाहरण: Character Type

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

Comparing Floats Safely

चूंकि floating-point math tiny rounding errors introduce कर सकता है, दो floats को == से compare करना जोखिम भरा है। एक safer approach यह जांचता है कि उनके बीच का difference एक छोटी tolerance value से कम है।

उदाहरण: Comparing Floats Safely

markup
// 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);
}
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. exact equality के लिए floating-point numbers को == से compare करना, जब tiny rounding errors उन्हें unequal बना सकती हैं।
  2. यह मान लेना कि Rust में char C की तरह एक byte है -- एक Rust char असल में एक 4-byte Unicode scalar value है।
  3. यह भूल जाना कि Rust में boolean values सख्ती से true/false हैं, कुछ दूसरी languages की तरह 0/1 integers नहीं।
चैप्टर सारांश
  • f32 और f64 floating-point numbers represent करते हैं; f64 default है और इसमें ज़्यादा precision है।
  • bool values बिल्कुल 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:

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.