use والمسارات
استيراد العناصر
use والمسارات درس مجاني في Learn Rust Coding على CoddyKit. هذا هو الدرس 3 من أصل 4. يمكنك قراءة الدرس كاملاً أدناه مجاناً — ثم تمرن عليه مباشرة في المتصفح باستخدام محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7. هذا الدرس جزء من مسار التعلم في Learn Rust Coding، وتقدمك يتزامن عبر الويب وتطبيق CoddyKit. تتضمن دورة Learn Rust Coding 4 دروس في المجموع.
بعض أجزاء هذا الدرس لم تُترجم بعد وتظهر باللغة الإنجليزية.
Bringing Items Into Scope
Typing full paths like std::collections::HashMap everywhere is tedious. The use keyword brings an item into scope so you can refer to it by a short name.
A Basic use
After a use, you reference the item by its final name. Here we import HashMap and use it directly.
use std::collections::HashMap;
fn main() {
let mut scores = HashMap::new();
scores.insert("ana", 10);
println!("{:?}", scores.get("ana"));
}Absolute vs Relative Paths
An absolute path starts from crate (the crate root). A relative path starts from the current module, optionally using self or super.
mod a {
pub mod b {
pub fn hi() { println!("hi from b"); }
}
pub fn call_b() {
crate::a::b::hi();
self::b::hi();
}
}
fn main() {
a::call_b();
}use Inside Modules
A use only affects the scope it appears in. Place it at the top of a module to shorten names used throughout that module.
mod work {
use std::collections::HashSet;
pub fn run() {
let mut s = HashSet::new();
s.insert(1);
s.insert(1);
println!("size {}", s.len());
}
}
fn main() {
work::run();
}Renaming With as
When two items share a name, or a name is too long, rename on import with as.
use std::collections::HashMap as Map;
fn main() {
let mut m: Map<&str, i32> = Map::new();
m.insert("x", 1);
println!("{:?}", m.get("x"));
}Grouping Imports
Import several items from the same path in one line using braces. This keeps the import block tidy.
use std::collections::{HashMap, HashSet};
fn main() {
let map: HashMap<i32, i32> = HashMap::new();
let set: HashSet<i32> = HashSet::new();
println!("{} {}", map.len(), set.len());
}Nested Groups and self
Inside a group, self imports the module itself alongside its items. This lets you use both the module name and specific members.
use std::io::{self, Write};
fn main() {
let _ = io::stdout().write_all(b"hello\n");
}Glob Imports
The glob * brings in everything public from a path. Use it sparingly — it can hide where names come from — but it is common for preludes and tests.
mod colors {
pub fn red() -> &'static str { "red" }
pub fn blue() -> &'static str { "blue" }
}
use colors::*;
fn main() {
println!("{} {}", red(), blue());
}Idiomatic Import Style
Convention: import functions by their parent module (use std::cmp; cmp::max(...)) but import types by name (use std::collections::HashMap;). This makes calls read clearly.
use std::cmp;
fn main() {
println!("{}", cmp::max(3, 9));
}Paths to External Crates
For dependencies, the path starts with the crate name, for example rand::thread_rng. You list the crate in Cargo.toml, then import items the same way you do for std.
Keeping Imports Clean
Group related imports, rename to avoid clashes, and prefer explicit imports over globs in application code. Clean imports make a file easy to scan.
Quick Check
Test your path knowledge.
Recap
You learned to import items:
usebrings an item into scope by a short name- Absolute paths start at
crate; relative paths useself/super asrenames, braces group, and*globs- Import types by name and functions by parent module for clarity
الأسئلة الشائعة
هل درس «use والمسارات» مجاني؟
نعم — نص درس «use والمسارات» كامل متاح مجاناً هنا على الويب. لتمرينه بشكل تفاعلي (محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7) وفتح باقي دورة Learn Rust Coding، انتقل إلى CoddyKit PRO. تتضمن دورة Learn Rust Coding 4 دروس في المجموع.
ماذا ستتعلم في «use والمسارات»؟
استيراد العناصر تتمرن على Learn Rust Coding مع أكواد عملية تشغلها مباشرة في المتصفح، ومدرس ذكاء اصطناعي متاح 24/7 يجيب على أسئلتك أثناء عملك.
هل أحتاج إلى خبرة سابقة لأبدأ Learn Rust Coding؟
لا تُشترط خبرة سابقة. Learn Rust Coding على CoddyKit منظم للمبتدئين حتى المتقدمين، لذا يمكنك البدء من هنا أو من البداية والتقدم بسرعتك الخاصة. هذا هو الدرس 3 من أصل 4.
كم من الوقت يستغرق درس «use والمسارات»؟
معظم دروس CoddyKit تستغرق حوالي 5–10 دقائق. كل منها موجز وتفاعلي، لذا تحرز تقدماً مستمراً وتستأنف من حيث توقفت عبر الويب والتطبيق.
هل يمكنني كتابة وتشغيل أكواد في درس Learn Rust Coding هذا؟
نعم. كل درس في Learn Rust Coding يتضمن محرر أكواد مدمج، لذا تكتب وتشغل أكواداً حقيقية مباشرة في متصفحك وتحصل على تعليقات فورية من الذكاء الاصطناعي — بدون إعداد محلي.
جميع الدروس في هذه الدورة
- الوحدات وmod
- pub وإمكانية الوصول
- use والمسارات
- Crates وجذر Crate