← Back to Rust Course | Chapter 5: Ownership | Lesson 4 of 6

Stack और Heap में अंतर

Stack छोटी, तेज़ चीज़ों के लिए trays के ढेर जैसा है, जबकि heap बड़ी चीज़ों के लिए एक बड़ा storage room है जो बढ़ सकता है।
Syntax
rust
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

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

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

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

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

markup
fn main() {
    {
        let boxed = String::from("owned heap data");
        println!("{}", boxed);
    } // heap memory freed automatically here
    println!("Scope ended, memory reclaimed");
}

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

markup
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);
}
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 में सारा data heap पर रहता है -- integers जैसी fixed-size, Copy values stack पर रहती हैं।
  2. यह सोचना कि String खुद पूरी तरह heap पर रहता है -- सिर्फ इसका character data रहता है; String struct का pointer, length, और capacity stack पर रहते हैं।
  3. यह मानना कि 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:

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.