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

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:

主站蜘蛛池模板: 许昌市| 光山县| 门源| 石屏县| 万载县| 松阳县| 兴义市| 清流县| 义马市| 宾川县| 吴江市| 通江县| 永登县| 伊通| 惠来县| 吴堡县| 拜城县| 钟山县| 资兴市| 云浮市| 台东县| 荣昌县| 庄河市| 南川市| 营口市| 红河县| 阿克| 社旗县| 章丘市| 琼中| 蒙自县| 金溪县| 南溪县| 南开区| 沙田区| 保定市| 长岛县| 宜春市| 贡觉县| 宣恩县| 江西省|