Constants क्या हैं
In this page:
const NAME: Type = value;
Declaring a Constant
Constants const keyword से declare होते हैं, हमेशा एक explicit type चाहते हैं, और कभी mutate नहीं हो सकते। ये उन values को represent करते हैं जो program की पूरी run के लिए fixed हैं।
उदाहरण: Declaring a Constant
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);
}
Login to try C/C++/Java/PHP code in the editor
Naming Convention
एक strong convention से, Rust constants SCREAMING_SNAKE_CASE उपयोग करके नाम दिए जाते हैं -- words के बीच underscores के साथ सभी uppercase letters। यह constants को regular variables से एक नज़र में visually अलग करता है।
उदाहरण: Naming Convention
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);
}
Login to try C/C++/Java/PHP code in the editor
Constants at Global Scope
let bindings के विपरीत, constants किसी function के बाहर, किसी file के top level पर declare किए जा सकते हैं, उन्हें पूरे module में accessible बनाते हुए।
उदाहरण: Constants at Global Scope
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);
}
Login to try C/C++/Java/PHP code in the editor
Constants vs Variables
Constants immutable let variables से तीन तरीकों में अलग हैं: उन्हें हमेशा एक type चाहिए, उन्हें global scope में declare किया जा सकता है, और runtime पर compute होने के बजाय उनकी value compile time पर जानी जानी चाहिए।
उदाहरण: Constants vs Variables
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);
}
Login to try C/C++/Java/PHP code in the editor
- Rust के
SCREAMING_SNAKE_CASEconvention के बजाय constants के लिए lowercase नाम उपयोग करना। - यह भूल जाना कि constants को हमेशा एक explicit type annotation चाहिए,
letbindings के विपरीत। mutके साथ एक constant declare करने की कोशिश करना -- constants कभी mutable नहीं हो सकते,letvariables के विपरीत।
- 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: