العمل مع HashSet
تتبّع القيم الفريدة
العمل مع HashSet درس مجاني في Learn Rust Coding على CoddyKit. هذا هو الدرس 3 من أصل 4. يمكنك قراءة الدرس كاملاً أدناه مجاناً — ثم تمرن عليه مباشرة في المتصفح باستخدام محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7. هذا الدرس جزء من مسار التعلم في Learn Rust Coding، وتقدمك يتزامن عبر الويب وتطبيق CoddyKit. تتضمن دورة Learn Rust Coding 4 دروس في المجموع.
بعض أجزاء هذا الدرس لم تُترجم بعد وتظهر باللغة الإنجليزية.
What Is a HashSet?
A HashSet stores a collection of unique values. There are no duplicates and no keys, just members.
It is great for answering one question fast: "have I seen this value before?"
Creating a Set
Like HashMap, HashSet lives in std::collections and needs a use import.
Create an empty one with HashSet::new() and declare it mut to add items.
use std::collections::HashSet;
fn main() {
let mut s: HashSet<i32> = HashSet::new();
s.insert(1);
s.insert(2);
println!("size: {}", s.len());
}Inserting and Duplicates
The insert method returns a bool: true if the value was new, false if it was already present.
Inserting a duplicate does nothing, so the set stays unique automatically.
use std::collections::HashSet;
fn main() {
let mut s = HashSet::new();
println!("{}", s.insert(5));
println!("{}", s.insert(5));
println!("len = {}", s.len());
}Membership Tests
Use contains to check whether a value is in the set. It returns a bool.
This is the most common reason to reach for a set: fast, clear lookups.
use std::collections::HashSet;
fn main() {
let mut s = HashSet::new();
s.insert("red");
s.insert("blue");
println!("has red? {}", s.contains("red"));
println!("has green? {}", s.contains("green"));
}Removing Members
The remove method takes a value and returns true if it was present and removed.
Removing something that is not there simply returns false.
use std::collections::HashSet;
fn main() {
let mut s = HashSet::new();
s.insert(7);
println!("{}", s.remove(&7));
println!("{}", s.remove(&7));
}Building From an Iterator
You can collect any iterator of values directly into a HashSet.
This is a quick way to deduplicate: feed in a list with repeats and the set keeps each value once.
use std::collections::HashSet;
fn main() {
let nums = [1, 2, 2, 3, 3, 3];
let unique: HashSet<i32> = nums.iter().copied().collect();
println!("distinct: {}", unique.len());
}Iterating a Set
Loop over a set with a for loop to visit each member.
As with HashMap, the iteration order is not guaranteed and may differ each run.
use std::collections::HashSet;
fn main() {
let s: HashSet<i32> = [10, 20, 30].into_iter().collect();
let mut total = 0;
for v in &s {
total += v;
}
println!("sum = {}", total);
}Union
The union method gives every value that is in either set.
It returns an iterator, so collect it into a new set or loop over it.
use std::collections::HashSet;
fn main() {
let a: HashSet<i32> = [1, 2, 3].into_iter().collect();
let b: HashSet<i32> = [3, 4].into_iter().collect();
let u: HashSet<i32> = a.union(&b).copied().collect();
println!("union size: {}", u.len());
}Intersection
The intersection method yields values found in both sets.
It is perfect for finding common elements, like shared tags or mutual friends.
use std::collections::HashSet;
fn main() {
let a: HashSet<i32> = [1, 2, 3].into_iter().collect();
let b: HashSet<i32> = [2, 3, 4].into_iter().collect();
let common: Vec<i32> = a.intersection(&b).copied().collect();
println!("common count: {}", common.len());
}Difference
The difference method yields values in the first set but not in the second.
Swap the order to get the opposite difference. There is also symmetric_difference for items in exactly one set.
use std::collections::HashSet;
fn main() {
let a: HashSet<i32> = [1, 2, 3].into_iter().collect();
let b: HashSet<i32> = [2].into_iter().collect();
let only_a: Vec<i32> = a.difference(&b).copied().collect();
println!("only in a: {}", only_a.len());
}Subset and Disjoint
Use is_subset to check if every member of one set is in another.
Use is_disjoint to test that two sets share no values at all. Both return a plain bool.
use std::collections::HashSet;
fn main() {
let a: HashSet<i32> = [1, 2].into_iter().collect();
let b: HashSet<i32> = [1, 2, 3].into_iter().collect();
println!("a subset of b? {}", a.is_subset(&b));
}Quick Check
You insert the same value twice into a HashSet.
Recap
A HashSet holds unique values with fast contains checks.
You inserted, removed, deduplicated via collect, and combined sets with union, intersection, and difference.
الأسئلة الشائعة
هل درس «العمل مع HashSet» مجاني؟
نعم — نص درس «العمل مع HashSet» كامل متاح مجاناً هنا على الويب. لتمرينه بشكل تفاعلي (محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7) وفتح باقي دورة Learn Rust Coding، انتقل إلى CoddyKit PRO. تتضمن دورة Learn Rust Coding 4 دروس في المجموع.
ماذا ستتعلم في «العمل مع HashSet»؟
تتبّع القيم الفريدة تتمرن على Learn Rust Coding مع أكواد عملية تشغلها مباشرة في المتصفح، ومدرس ذكاء اصطناعي متاح 24/7 يجيب على أسئلتك أثناء عملك.
هل أحتاج إلى خبرة سابقة لأبدأ Learn Rust Coding؟
لا تُشترط خبرة سابقة. Learn Rust Coding على CoddyKit منظم للمبتدئين حتى المتقدمين، لذا يمكنك البدء من هنا أو من البداية والتقدم بسرعتك الخاصة. هذا هو الدرس 3 من أصل 4.
كم من الوقت يستغرق درس «العمل مع HashSet»؟
معظم دروس CoddyKit تستغرق حوالي 5–10 دقائق. كل منها موجز وتفاعلي، لذا تحرز تقدماً مستمراً وتستأنف من حيث توقفت عبر الويب والتطبيق.
هل يمكنني كتابة وتشغيل أكواد في درس Learn Rust Coding هذا؟
نعم. كل درس في Learn Rust Coding يتضمن محرر أكواد مدمج، لذا تكتب وتشغل أكواداً حقيقية مباشرة في متصفحك وتحصل على تعليقات فورية من الذكاء الاصطناعي — بدون إعداد محلي.
جميع الدروس في هذه الدورة
- إنشاء HashMap
- واجهة Entry والقيم الافتراضية
- العمل مع HashSet
- العدّ والتجميع