Passing imp DS(vec,map,set) to function
Amit Nadiger
Polyglot(Rust??, C++ 11,14,17,20, C, Kotlin, Java) Android TV, Cas, Blockchain, Polkadot, UTXO, Substrate, Wasm, Proxy-wasm,AndroidTV, Dvb, STB, Linux, Engineering management.
In Rust, we can pass imp data structures such as Vec<T>, HashMap<K, V>, and HashSet<T> to functions in different ways, depending on whether you want to:
1. Passing a Vec<T> (Vector)
A vector (Vec<T>) is a growable array type.
// Takes ownership and moves the vector.
fn take_vector(vec: Vec<i32>) {
println!("Vector moved into function: {:?}", vec);
}
// Takes an immutable reference.
fn borrow_vector(vec: &Vec<i32>) {
println!("Borrowed vector: {:?}", vec);
}
// Takes a mutable reference and modifies the vector.
fn modify_vector(vec: &mut Vec<i32>) {
vec.push(108);
println!("Modified vector: {:?}", vec);
}
fn main() {
let mut numbers = vec![1, 2, 3];
take_vector(numbers.clone()); // Pass ownership (move) but cloning to retain the original
borrow_vector(&numbers); // Pass by immutable reference
modify_vector(&mut numbers); // Pass by mutable reference
println!("Vector after modification: {:?}", numbers);
}
/*
Vector moved into function: [1, 2, 3]
Borrowed vector: [1, 2, 3]
Modified vector: [1, 2, 3, 108]
Vector after modification: [1, 2, 3, 42]
*/
2. Passing a HashMap<K, V> (Key-Value Map)
A HashMap<K, V> is a collection of key-value pairs.
use std::collections::HashMap;
fn take_map(map: HashMap<String, i32>) {
println!("Map moved into function: {:?}", map);
}
fn borrow_map(map: &HashMap<String, i32>) {
println!("Borrowed map: {:?}", map);
}
fn modify_map(map: &mut HashMap<String, i32>) {
map.insert("d".to_string(), 40);
println!("Modified map: {:?}", map);
}
fn main() {
let mut scores = HashMap::new();
scores.insert("a".to_string(), 10);
scores.insert("b".to_string(), 20);
scores.insert("c".to_string(), 30);
take_map(scores.clone()); // Pass by value (move), cloning to retain the original
borrow_map(&scores); // Pass by immutable reference
modify_map(&mut scores); // Pass by mutable reference
println!("Map after modification: {:?}", scores);
}
/*
Map moved into function: {"a": 10, "b": 20, "c": 30}
Borrowed map: {"a": 10, "b": 20, "c": 30}
Modified map: {"a": 10, "b": 20, "d": 40, "c": 30}
Map after modification: {"a": 10, "b": 20, "d": 40, "c": 30}
*/
3. Passing a HashSet<T> (Set)
A HashSet<T> is a collection of unique values.
use std::collections::HashSet;
fn take_set(set: HashSet<i32>) {
println!("Set moved into function: {:?}", set);
}
fn borrow_set(set: &HashSet<i32>) {
println!("Borrowed set: {:?}", set);
}
fn modify_set(set: &mut HashSet<i32>) {
set.insert(108);
println!("Modified set: {:?}", set);
}
fn main() {
let mut numbers: HashSet<i32> = vec![1, 2, 3].into_iter().collect();
take_set(numbers.clone()); // Pass by value (move), cloning to retain the original
borrow_set(&numbers); // Pass by immutable reference
modify_set(&mut numbers); // Pass by mutable reference
println!("Set after modification: {:?}", numbers);
}
/*
Set moved into function: {1, 3, 2}
Borrowed set: {1, 3, 2}
Modified set: {1, 2, 108, 3}
Set after modification: {1, 2, 108, 3}
*/