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

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:

主站蜘蛛池模板: 江津市| 宁津县| 金寨县| 化隆| 大关县| 霞浦县| 丁青县| 沂源县| 巴塘县| 资兴市| 东丽区| 湘阴县| 西乌| 绥芬河市| 安顺市| 方正县| 定远县| 枣阳市| 双辽市| 宜城市| 张家港市| 婺源县| 舞钢市| 个旧市| 五峰| 绥德县| 志丹县| 万山特区| 兰州市| 广南县| 平阴县| 高碑店市| 沈阳市| 龙泉市| 佳木斯市| 仙桃市| 章丘市| 留坝县| 密山市| 独山县| 江孜县|