← Back to Rust Course | Chapter 15: Cargo, Testing & Best Practices | Lesson 2 of 8

crates.io से Crates का उपयोग

crates.io ready-made Rust code की एक बड़ी public shelf है जिसे कोई भी अपने project में जोड़ सकता है।
Syntax
rust
[dependencies]
crate_name = "1.0"
crate_name = { version = "1.0", features = ["feature"] }

use crate_name::item;

What crates.io Is

crates.io central, official registry है जहां Rust community reusable packages publish करती है, जिन्हें crates कहते हैं, जिन पर कोई भी project Cargo के ज़रिए depend कर सकता है।

उदाहरण: What crates.io Is

markup
// Entry point: execution of the program starts here
fn main() {
    // Print "crates.io hosts community packages like serde, rand, and clap" to the console (with a trailing newline)
    println!("crates.io hosts community packages like serde, rand, and clap");
}

Adding a Dependency Manually

crates.io से एक crate उपयोग करने के लिए, आप Cargo.toml में [dependencies] के तहत एक entry जोड़ते हैं इसका नाम और version requirement specify करते हुए।

उदाहरण: Adding a Dependency Manually

bash
[dependencies]
rand = "0.8"

⚠️ Run this in your own terminal or Node.js environment.

Using cargo add

cargo add subcommand अपने आप किसी crate का latest compatible version ढूंढता है और इसे आपके लिए Cargo.toml में जोड़ता है, बिना manual editing के।

उदाहरण: Using cargo add

bash
cargo add rand

⚠️ Run this in your own terminal or Node.js environment.

Standard Library Only in This Course

चूंकि sandboxed code runners (इन examples को चला रहे वाला सहित) अक्सर crates fetch करने के लिए network access की कमी रखते हैं, इस course में हर runnable example जानबूझकर सिर्फ Rust standard library उपयोग करता है।

उदाहरण: Standard Library Only in This Course

markup
// Entry point: execution of the program starts here
fn main() {
    // Print "This course sticks to the standard library so every example can run offline" to the console (with a trailing newline)
    println!("This course sticks to the standard library so every example can run offline");
}
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
आम गलतियां
  1. यह मान लेना कि कोई crate सिर्फ आपके code में use लिखने से अपने आप आपके project में जुड़ जाता है -- इसे पहले Cargo.toml में declare किया जाना चाहिए।
  2. इसकी version compatibility जांचे बिना एक crate जोड़ना, जो अनजाने में breaking changes ला सकता है।
  3. यह भूल जाना कि network access के बिना Judge0-style sandboxes crates download नहीं कर सकते, इसलिए वहां के exercises को सिर्फ standard library पर भरोसा करना चाहिए।
चैप्टर सारांश
  • crates.io Rust packages (crates) के लिए official public registry है।
  • किसी crate को Cargo.toml में एक [dependencies] entry के रूप में जोड़ना इसे आपके code में use के लिए available बनाता है।
  • cargo add crate_name बिना manually Cargo.toml edit किए एक dependency जोड़ने का एक सुविधाजनक तरीका है।
  • serde (serialization) और rand (random numbers) जैसे popular crates यह extend करते हैं कि अकेली standard library क्या कर सकती है।
🔒

Chapter Quiz — Complete all 8 topics to unlock

0/8 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.