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

- Effective C#:改善C#代碼的50個有效方法(原書第3版)
- Visual Basic編程:從基礎到實踐(第2版)
- 數據結構簡明教程(第2版)微課版
- Visual Basic程序設計習題解答與上機指導
- Lua程序設計(第4版)
- HTML5+CSS3網頁設計
- 單片機應用與調試項目教程(C語言版)
- Spring Boot企業級項目開發實戰
- Building Dynamics CRM 2015 Dashboards with Power BI
- 玩轉.NET Micro Framework移植:基于STM32F10x處理器
- Application Development with Parse using iOS SDK
- 快樂編程:青少年思維訓練
- Go Systems Programming
- Ubuntu Server Cookbook
- C語言程序設計實驗指導與習題精解