← Back to Rust Course | Chapter 9: Collections | Lesson 1 of 6

Vec के Basics

एक Vec एक list है जो आपके चीज़ें जोड़ने या हटाने पर बढ़ या सिकुड़ सकती है।
Syntax
rust
let mut name: Vec<Type> = Vec::new();
let name = vec![v1, v2, v3];
name.push(value);
name[index]
name.get(index)

Creating a Vec

एक Vec या तो एक खाली list के लिए Vec::new() से बनाया जाता है या initial elements सीधे list करने के लिए vec! macro से।

उदाहरण: Creating a Vec

markup
// Entry point: execution of the program starts here
fn main() {
    // Declare an immutable variable `numbers`, set to `vec![1, 2, 3]`
    let numbers = vec![1, 2, 3];
    // Print "{:?}" to the console, substituting in `numbers` (with a trailing newline)
    println!("{:?}", numbers);
}

Pushing and Popping

.push() एक mutable Vec के आखिर में एक element जोड़ता है, और .pop() आखिरी element हटाता और return करता है, एक Option में wrapped क्योंकि vector खाली हो सकता है।

उदाहरण: Pushing and Popping

markup
// Entry point: execution of the program starts here
fn main() {
    // Declare a mutable variable `stack`, initialized to `Vec::new()`
    let mut stack = Vec::new();
    // Append `10` to `stack`
    stack.push(10);
    // Append `20` to `stack`
    stack.push(20);
    // Declare an immutable variable `last`, set to `stack.pop()`
    let last = stack.pop();
    // Print "Popped: {:?}, remaining: {:?}" to the console, substituting in `last, stack` (with a trailing newline)
    println!("Popped: {:?}, remaining: {:?}", last, stack);
}

Accessing Elements Safely

[] से direct indexing panic करता है अगर index bounds से बाहर हो। .get(index) इसके बजाय एक Option return करता है, आपको बिना crash हुए एक missing index handle करने देते हुए।

उदाहरण: Accessing Elements Safely

markup
// Entry point: execution of the program starts here
fn main() {
    // Declare an immutable variable `items`, set to `vec!["a", "b", "c"]`
    let items = vec!["a", "b", "c"];
    // Print "{:?}" to the console, substituting in `items.get(1)` (with a trailing newline)
    println!("{:?}", items.get(1));
    // Print "{:?}" to the console, substituting in `items.get(10)` (with a trailing newline)
    println!("{:?}", items.get(10));
}

Iterating Over a Vec

एक for loop किसी Vec के elements पर सीधे reference से iterate कर सकता है, आपको पूरे vector की ownership लिए बिना हर item process करने देते हुए।

उदाहरण: Iterating Over a Vec

markup
// Entry point: execution of the program starts here
fn main() {
    // Declare an immutable variable `scores`, set to `vec![90, 85, 77]`
    let scores = vec![90, 85, 77];
    // Loop over `&scores`, binding each item to `score`
    for score in &scores {
        // Print "Score: {}" to the console, substituting in `score` (with a trailing newline)
        println!("Score: {}", score);
    }
}
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. किसी Vec के आखिर से आगे index करने की कोशिश करना, जो runtime पर panic करता है एक default value return करने के बजाय।
  2. यह भूल जाना कि इसमें नए elements push करने से पहले Vec::new() को binding पर mut चाहिए।
  3. vec[i] उपयोग करना जब index bounds से बाहर हो सकता है, safer .get(i) के बजाय जो एक Option return करता है।
चैप्टर सारांश
  • Vec<T> T type के elements की एक growable, heap-allocated list है।
  • vec![...] initial elements के साथ एक Vec बनाने के लिए एक convenient macro है।
  • .push() आखिर में एक element जोड़ता है; .pop() आखिरी वाला हटाता और return करता है।
  • [] से indexing एक out-of-bounds index पर panic करता है, जबकि .get() सुरक्षित रूप से Option return करता है।
🔒

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.