أساسيات Cargo
البناء والتشغيل
أساسيات Cargo درس مجاني في Learn Rust Coding على CoddyKit. هذا هو الدرس 1 من أصل 4. يمكنك قراءة الدرس كاملاً أدناه مجاناً — ثم تمرن عليه مباشرة في المتصفح باستخدام محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7. هذا الدرس جزء من مسار التعلم في Learn Rust Coding، وتقدمك يتزامن عبر الويب وتطبيق CoddyKit. تتضمن دورة Learn Rust Coding 4 دروس في المجموع.
بعض أجزاء هذا الدرس لم تُترجم بعد وتظهر باللغة الإنجليزية.
What Is Cargo?
Cargo is Rust's build system and package manager. It compiles your code, downloads dependencies, runs tests, and builds documentation.
- Comes bundled with the Rust toolchain
- Manages a project's
Cargo.tomlmanifest - Handles the whole build lifecycle for you
Creating a New Project
Use cargo new to scaffold a binary project. It creates a directory with a src/main.rs and a Cargo.toml.
cargo new my_appmakes a binary cratecargo new my_lib --libmakes a library crate
cargo new my_app
cd my_appThe Default main.rs
A fresh binary project starts with a tiny main.rs. The main function is the entry point of every Rust binary.
fn main() {
println!("Hello, world!");
}Building with cargo build
cargo build compiles your project into the target/debug directory. The first build also resolves and compiles dependencies.
- Debug builds are unoptimized but fast to compile
- The binary lands in
target/debug/my_app
cargo buildRunning with cargo run
cargo run builds (if needed) and then runs the binary in one step. It's the fastest way to iterate during development.
fn main() {
let name = "Cargo";
println!("Running with {name}!");
}Checking with cargo check
cargo check analyzes your code for errors without producing a binary. It is much faster than a full build and ideal for tight edit-compile loops.
cargo checkRelease Builds
Add --release to enable optimizations. Release builds are slower to compile but produce faster binaries placed in target/release.
cargo build --release
cargo run --releasePassing Arguments
Arguments after -- are passed to your program, not to Cargo. Read them with std::env::args.
use std::env;
fn main() {
let args: Vec<String> = env::args().collect();
println!("Got {} arguments", args.len());
for arg in &args {
println!("{arg}");
}
}Cleaning Artifacts
cargo clean deletes the target directory, removing all compiled output. Use it to force a fresh build or reclaim disk space.
cargo cleanThe Cargo.lock File
Cargo records the exact resolved dependency versions in Cargo.lock.
- For binaries: commit it for reproducible builds
- For libraries: usually not committed
- It is generated and updated automatically
Common Cargo Commands
A quick reference of the everyday commands:
cargo new— create a projectcargo build— compilecargo run— build and runcargo check— fast error checkcargo test— run testscargo doc— build docs
Quick Check
Which command compiles your code without producing an executable binary?
Recap
You learned the core Cargo workflow:
cargo newscaffolds projectscargo buildcompiles,cargo runbuilds and runscargo checkis the fast error check--releaseenables optimizationsCargo.lockpins dependency versions
Cargo is the backbone of every Rust project.
الأسئلة الشائعة
هل درس «أساسيات Cargo» مجاني؟
نعم — نص درس «أساسيات Cargo» كامل متاح مجاناً هنا على الويب. لتمرينه بشكل تفاعلي (محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7) وفتح باقي دورة Learn Rust Coding، انتقل إلى CoddyKit PRO. تتضمن دورة Learn Rust Coding 4 دروس في المجموع.
ماذا ستتعلم في «أساسيات Cargo»؟
البناء والتشغيل تتمرن على Learn Rust Coding مع أكواد عملية تشغلها مباشرة في المتصفح، ومدرس ذكاء اصطناعي متاح 24/7 يجيب على أسئلتك أثناء عملك.
هل أحتاج إلى خبرة سابقة لأبدأ Learn Rust Coding؟
لا تُشترط خبرة سابقة. Learn Rust Coding على CoddyKit منظم للمبتدئين حتى المتقدمين، لذا يمكنك البدء من هنا أو من البداية والتقدم بسرعتك الخاصة. هذا هو الدرس 1 من أصل 4.
كم من الوقت يستغرق درس «أساسيات Cargo»؟
معظم دروس CoddyKit تستغرق حوالي 5–10 دقائق. كل منها موجز وتفاعلي، لذا تحرز تقدماً مستمراً وتستأنف من حيث توقفت عبر الويب والتطبيق.
هل يمكنني كتابة وتشغيل أكواد في درس Learn Rust Coding هذا؟
نعم. كل درس في Learn Rust Coding يتضمن محرر أكواد مدمج، لذا تكتب وتشغل أكواداً حقيقية مباشرة في متصفحك وتحصل على تعليقات فورية من الذكاء الاصطناعي — بدون إعداد محلي.
جميع الدروس في هذه الدورة
- أساسيات Cargo
- Cargo.toml
- مساحات العمل
- الميزات والملفات التعريفية