crates.io से Crates का उपयोग
In this page:
[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
// 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");
}
Login to try C/C++/Java/PHP code in the editor
Adding a Dependency Manually
crates.io से एक crate उपयोग करने के लिए, आप Cargo.toml में [dependencies] के तहत एक entry जोड़ते हैं इसका नाम और version requirement specify करते हुए।
उदाहरण: Adding a Dependency Manually
[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
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
// 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");
}
Login to try C/C++/Java/PHP code in the editor
- यह मान लेना कि कोई crate सिर्फ आपके code में
useलिखने से अपने आप आपके project में जुड़ जाता है -- इसे पहलेCargo.tomlमें declare किया जाना चाहिए। - इसकी version compatibility जांचे बिना एक crate जोड़ना, जो अनजाने में breaking changes ला सकता है।
- यह भूल जाना कि 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बिना manuallyCargo.tomledit किए एक 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: