Accessing elements
📖 Scenario: You are managing a small fruit basket in a kitchen. You want to check which fruits are inside and pick one to eat.
🎯 Goal: Learn how to access elements in a Rust vector by index.
📋 What You'll Learn
Create a vector called
fruits with these exact strings: "apple", "banana", "cherry"Create a variable called
index and set it to 1Access the fruit at position
index in the fruits vector and store it in a variable called selected_fruitPrint the
selected_fruit to the console💡 Why This Matters
🌍 Real World
Accessing elements by index is common when working with lists of items, like selecting a product from a shopping list or a song from a playlist.
💼 Career
Many programming jobs require working with collections of data and retrieving specific items efficiently.
Progress0 / 4 steps
