官术网_书友最值得收藏!

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:

主站蜘蛛池模板: 临沭县| 措美县| 台湾省| 宝丰县| 嘉定区| 永州市| 股票| 分宜县| 盐津县| 永昌县| 木里| 淳化县| 保定市| 萝北县| 德化县| 华安县| 东阿县| 盱眙县| 桐庐县| 右玉县| 乌兰县| 长寿区| 淮滨县| 铁岭市| 额敏县| 丘北县| 东辽县| 海兴县| 普兰县| 临湘市| 河津市| 合江县| 九台市| 广河县| 陕西省| 鸡东县| 汨罗市| 阜平县| 巨野县| 吉木萨尔县| 罗山县|