Stack और Heap में अंतर
In this page:
let stack_value: i32 = 5;
let heap_value = Box::new(value);
let heap_string = String::from("text");
What Lives on the Stack
Compile time पर पता fixed-size values, जैसे integers, floats, booleans, और chars, stack पर store होते हैं। Stack allocation बेहद तेज़ है क्योंकि यह सिर्फ एक pointer ऊपर या नीचे move करता है।
उदाहरण: What Lives on the Stack
// Entry point: execution of the program starts here
fn main() {
// Declare an immutable variable `a` (an integer), set to `10`
let a: i32 = 10;
// Declare an immutable variable `b` (a floating-point number), set to `3.14`
let b: f64 = 3.14;
// Print "Stack values: {} and {}" to the console, substituting in `a, b` (with a trailing newline)
println!("Stack values: {} and {}", a, b);
}
Login to try C/C++/Java/PHP code in the editor
What Lives on the Heap
Data जिसका size runtime पर बढ़ सकता है, जैसे एक String या Vec की contents, heap पर allocate होता है। Variable खुद एक छोटा stack-based header रखता है जो उस heap data की ओर point करता है।
उदाहरण: What Lives on the Heap
// Entry point: execution of the program starts here
fn main() {
// Declare a mutable variable `heap_data`, initialized to `String::from("start")`
let mut heap_data = String::from("start");
heap_data.push_str(" grows on the heap");
// Print "{}" to the console, substituting in `heap_data` (with a trailing newline)
println!("{}", heap_data);
}
Login to try C/C++/Java/PHP code in the editor
Why the Distinction Matters for Ownership
Ownership rules बड़े पैमाने पर heap data को सुरक्षित रूप से manage करने के लिए मौजूद हैं: जब एक owning variable drop होता है, Rust corresponding heap allocation को exactly एक बार free करता है, memory leaks और double frees रोकते हुए।
उदाहरण: Why the Distinction Matters for Ownership
fn main() {
{
let boxed = String::from("owned heap data");
println!("{}", boxed);
} // heap memory freed automatically here
println!("Scope ended, memory reclaimed");
}
Login to try C/C++/Java/PHP code in the editor
Stack Is Faster, Heap Is Flexible
चूंकि stack सिर्फ एक end पर बढ़ता या सिकुड़ता है, इससे push और pop करना बहुत सस्ता है। Heap flexible, dynamically-sized allocations allow करता है, allocate और access करने में थोड़ा ज़्यादा overhead की कीमत पर।
उदाहरण: Stack Is Faster, Heap Is Flexible
fn main() {
let stack_value = 42; // fast, fixed size
let heap_value = vec![1, 2, 3, 4]; // flexible, can grow
println!("{} and {:?}", stack_value, heap_value);
}
Login to try C/C++/Java/PHP code in the editor
- यह मान लेना कि Rust में सारा data heap पर रहता है -- integers जैसी fixed-size,
Copyvalues stack पर रहती हैं। - यह सोचना कि
Stringखुद पूरी तरह heap पर रहता है -- सिर्फ इसका character data रहता है;Stringstruct का pointer, length, और capacity stack पर रहते हैं। - यह मानना कि heap allocation हमेशा धीमा है और इससे पूरी तरह बचना चाहिए, जब यह सिर्फ उस data के लिए ज़रूरी है जिसका size बढ़ सकता है।
- Stack fixed-size data को तेज़ी से store करता है, last-in-first-out order में; heap उस data को store करता है जिसका size बढ़ सकता है या compile time पर अज्ञात है।
StringऔरVecजैसे types stack पर एक छोटा fixed-size header रखते हैं जो heap पर variable-size data की ओर point करता है।- Stack allocation और deallocation बहुत तेज़ है; heap allocation में ज़्यादा bookkeeping शामिल है और तुलनात्मक रूप से धीमा है।
- Ownership rules दोनों पर लागू होते हैं, लेकिन heap-allocated memory को बिल्कुल एक बार सही से free करने के लिए सबसे ज़्यादा मायने रखते हैं।
Chapter Quiz — Complete all 6 topics to unlock
0/6 topics done
Complete these topics first: