- Mastering Rust
- Rahul Sharma Vesa Kaihlavirta
- 149字
- 2021-07-02 13:35:20
Running examples with Cargo
To enable users to quickly get started with your crate, it's a good practice to communicate to users how to use your crate with code examples. Cargo standardize this practice, meaning that you can add an examples/ directory within your project root that can contain one or more .rs files, with a main function showing example usage of your crate.
The code under the examples/ directory can be run by using cargo run --examples <file_name>, where the filename is given without the .rs extension. To demonstrate this, we've added an example/ directory for our myexponent crate containing a file named basic.rs:
// myexponent/examples/basic.rs
use myexponent::pow;
fn main() {
println!("8 raised to 2 is {}", pow(8, 2));
}
Under the examples/ directory, we imported our pow function from our myexponent crate. The following is the output upon running cargo run --example basic:

- iOS Game Programming Cookbook
- Mastering OpenLayers 3
- TypeScript Essentials
- 單片機C語言程序設計實訓100例:基于STC8051+Proteus仿真與實戰
- 看透JavaScript:原理、方法與實踐
- Python程序設計
- PySide GUI Application Development(Second Edition)
- Java應用開發技術實例教程
- Learning Network Forensics
- Visual C++數字圖像處理技術詳解
- PHP+MySQL網站開發項目式教程
- Building an RPG with Unity 2018
- 零基礎學單片機C語言程序設計
- 基于Struts、Hibernate、Spring架構的Web應用開發
- Python預測分析實戰