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

Constants क्या हैं

एक constant एक value है जिसे एक permanent नाम मिलता है और program चलते समय इसे कभी नहीं बदला जा सकता।
Syntax
rust
const NAME: Type = value;

Declaring a Constant

Constants const keyword से declare होते हैं, हमेशा एक explicit type चाहते हैं, और कभी mutate नहीं हो सकते। ये उन values को represent करते हैं जो program की पूरी run के लिए fixed हैं।

उदाहरण: Declaring a Constant

markup
const MAX_SCORE: i32 = 100;

// Entry point: execution of the program starts here
fn main() {
    // Print "Maximum possible score is {}" to the console, substituting in `MAX_SCORE` (with a trailing newline)
    println!("Maximum possible score is {}", MAX_SCORE);
}

Naming Convention

एक strong convention से, Rust constants SCREAMING_SNAKE_CASE उपयोग करके नाम दिए जाते हैं -- words के बीच underscores के साथ सभी uppercase letters। यह constants को regular variables से एक नज़र में visually अलग करता है।

उदाहरण: Naming Convention

markup
const SECONDS_PER_MINUTE: i32 = 60;

// Entry point: execution of the program starts here
fn main() {
    // Print "A minute has {} seconds" to the console, substituting in `SECONDS_PER_MINUTE` (with a trailing newline)
    println!("A minute has {} seconds", SECONDS_PER_MINUTE);
}

Constants at Global Scope

let bindings के विपरीत, constants किसी function के बाहर, किसी file के top level पर declare किए जा सकते हैं, उन्हें पूरे module में accessible बनाते हुए।

उदाहरण: Constants at Global Scope

markup
const GREETING: &str = "Welcome";

// Entry point: execution of the program starts here
fn main() {
    // Print "{}, traveler!" to the console, substituting in `GREETING` (with a trailing newline)
    println!("{}, traveler!", GREETING);
}

Constants vs Variables

Constants immutable let variables से तीन तरीकों में अलग हैं: उन्हें हमेशा एक type चाहिए, उन्हें global scope में declare किया जा सकता है, और runtime पर compute होने के बजाय उनकी value compile time पर जानी जानी चाहिए।

उदाहरण: Constants vs Variables

markup
const PI_APPROX: f64 = 3.14159;

// Entry point: execution of the program starts here
fn main() {
    // Declare an immutable variable `radius`, set to `2.0`
    let radius = 2.0;
    // Print "Circle area: {}" to the console, substituting in `PI_APPROX * radius * radius` (with a trailing newline)
    println!("Circle area: {}", PI_APPROX * radius * radius);
}
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 के SCREAMING_SNAKE_CASE convention के बजाय constants के लिए lowercase नाम उपयोग करना।
  2. यह भूल जाना कि constants को हमेशा एक explicit type annotation चाहिए, let bindings के विपरीत।
  3. mut के साथ एक constant declare करने की कोशिश करना -- constants कभी mutable नहीं हो सकते, let variables के विपरीत।
चैप्टर सारांश
  • Constants const से declare होते हैं और उनमें एक explicit type annotation होना चाहिए।
  • Constants हमेशा immutable होते हैं -- उन्हें कभी mut चिह्नित नहीं किया जा सकता।
  • Convention से, constant नाम SCREAMING_SNAKE_CASE उपयोग करते हैं।
  • Constants किसी भी scope में declare किए जा सकते हैं, main के बाहर global scope सहित।
🔒

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.