Vec के Basics
Vec एक list है जो आपके चीज़ें जोड़ने या हटाने पर बढ़ या सिकुड़ सकती है।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
// 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);
}
Login to try C/C++/Java/PHP code in the editor
Pushing and Popping
.push() एक mutable Vec के आखिर में एक element जोड़ता है, और .pop() आखिरी element हटाता और return करता है, एक Option में wrapped क्योंकि vector खाली हो सकता है।
उदाहरण: Pushing and Popping
// 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);
}
Login to try C/C++/Java/PHP code in the editor
Accessing Elements Safely
[] से direct indexing panic करता है अगर index bounds से बाहर हो। .get(index) इसके बजाय एक Option return करता है, आपको बिना crash हुए एक missing index handle करने देते हुए।
उदाहरण: Accessing Elements Safely
// 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));
}
Login to try C/C++/Java/PHP code in the editor
Iterating Over a Vec
एक for loop किसी Vec के elements पर सीधे reference से iterate कर सकता है, आपको पूरे vector की ownership लिए बिना हर item process करने देते हुए।
उदाहरण: Iterating Over a Vec
// 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);
}
}
Login to try C/C++/Java/PHP code in the editor
- किसी
Vecके आखिर से आगे index करने की कोशिश करना, जो runtime पर panic करता है एक default value return करने के बजाय। - यह भूल जाना कि इसमें नए elements push करने से पहले
Vec::new()को binding परmutचाहिए। vec[i]उपयोग करना जब index bounds से बाहर हो सकता है, safer.get(i)के बजाय जो एकOptionreturn करता है।
Vec<T>Ttype के elements की एक growable, heap-allocated list है।vec![...]initial elements के साथ एकVecबनाने के लिए एक convenient macro है।.push()आखिर में एक element जोड़ता है;.pop()आखिरी वाला हटाता और return करता है।[]से indexing एक out-of-bounds index पर panic करता है, जबकि.get()सुरक्षित रूप सेOptionreturn करता है।
Chapter Quiz — Complete all 6 topics to unlock
0/6 topics done
Complete these topics first: