- 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:

- Getting started with Google Guava
- Azure IoT Development Cookbook
- Photoshop智能手機APP UI設計之道
- 零基礎學Scratch少兒編程:小學課本中的Scratch創意編程
- Git高手之路
- Object-Oriented JavaScript(Second Edition)
- Android底層接口與驅動開發技術詳解
- 精通Linux(第2版)
- VMware虛擬化技術
- 大話Java:程序設計從入門到精通
- Java 從入門到項目實踐(超值版)
- Instant Automapper
- Learning Unreal Engine Game Development
- 虛擬現實:引領未來的人機交互革命
- Serverless工程實踐:從入門到進階