Day 21:Learning Strings, Hash Maps, and Vectors in Rust.

Learner, Love to make things simple, Full Stack Developer, StackOverflower, Passionate about using machine learning, deep learning and AI
Search for a command to run...

Learner, Love to make things simple, Full Stack Developer, StackOverflower, Passionate about using machine learning, deep learning and AI
No comments yet. Be the first to comment.
Move beyond traditional RESTful thinking. Learn how to design APIs specifically for MCP (Model Context Protocol) servers. This guide covers the shift in mindset, a practical OpenAPI 3.1 example, and a Spring Boot implementation to make your services ...

Extending Kestra to Every Corner of Your Data Stack. Introduction: The Power of Plugins Imagine you're a master chef. You don't just have one knife - you have specialized tools for every task: a paring knife for delicate work, a chef's knife for chop...
Mastering Complex Orchestration Scenarios. Introduction: The Orchestrator's Toolkit Imagine you're conducting a symphony. You don't just wave your baton - you cue sections, adjust tempo, handle surprises, and ensure harmony. That's what advanced work...
From Data Extraction to Loading - A Practical Guide Introduction: Why ETL Still Matters in the Modern Data Stack Remember when data engineering was "extract, transform, load"? Some say ETL is dead, replaced by ELT, reverse ETL, and data mesh. But her...
Building Blocks of Declarative Orchestration. Introduction: The Power of Simplicity Imagine trying to build a house without understanding bricks, beams, and blueprints. That's what using an orchestration tool without understanding its core concepts f...
Rust provides a rich set of data structures, including strings, hash maps, and vectors, offering versatile solutions for data manipulation and storage. Understanding these data structures is fundamental for effective Rust programming. Let's delve into each of them to explore their features and usage.
Strings (String) in Rust are mutable, growable, and UTF-8 encoded. They can be created and initialized in various ways.
// Creating an empty string
let mut empty_string = String::new();
// Creating a string from a string literal
let greeting = String::from("Hello, Rust!");
// Concatenating strings
empty_string.push_str("Welcome ");
empty_string.push('t'); // Adding a single character
empty_string.push('o');
Strings in Rust offers several methods for manipulation, slicing, and formatting.
let mut message = String::from("Rust Programming");
// Replacing part of a string
message.replace_range(5..8, "is");
// Extracting a substring
let sub_string = &message[0..4]; // "Rust"
// Converting to uppercase
let upper_case = message.to_uppercase();
Hash maps (HashMap<K, V>) in Rust store key-value pairs, providing efficient lookup and insertion.
use std::collections::HashMap;
// Creating an empty hash map
let mut contacts = HashMap::new();
// Inserting values into the hash map
contacts.insert("Alice", "12345");
contacts.insert("Bob", "67890");
// Accessing values
if let Some(number) = contacts.get("Alice") {
println!("Alice's number: {}", number);
}
Hash maps offer methods to insert, update, retrieve, and remove elements efficiently.
// Updating a value
contacts.insert("Alice", "54321");
// Removing an entry
contacts.remove("Bob");
Vectors (Vec<T>) are resizable arrays that store elements of the same type in a contiguous memory block.
// Creating an empty vector
let mut numbers: Vec<i32> = Vec::new();
// Initializing a vector with elements
let mut fruits = vec!["Apple", "Banana", "Orange"];
// Adding elements to a vector
numbers.push(10);
numbers.push(20);
numbers.push(30);
Vectors offer various methods for adding, removing, accessing, and manipulating elements.
// Accessing elements
let first_element = numbers[0];
// Removing elements
let removed_element = numbers.pop();
// Iterating over elements
for fruit in &fruits {
println!("{}", fruit);
}
Understanding strings, hash maps, and vectors in Rust is essential for effective data manipulation and storage. Strings provide mutable, UTF-8 encoded text manipulation. Hash maps offer efficient key-value storage and retrieval. Vectors, as resizable arrays, enable dynamic storage and manipulation of elements. Leveraging these data structures appropriately enhances the efficiency and flexibility of Rust programs, making them robust and performant. Mastery of these fundamental data structures opens the door to building powerful and efficient Rust applications.
Happy coding with Rust!
I hope this helps, you!!
More such articles:
https://www.youtube.com/@maheshwarligade
\==========================**=========================
If this article adds any value to you then please clap and comment.
Let’s connect on Stackoverflow, LinkedIn, & Twitter.