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

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:

主站蜘蛛池模板: 泾阳县| 巴中市| 亳州市| 阿合奇县| 邳州市| 开远市| 青龙| 马关县| 钦州市| 兴化市| 朝阳县| 武平县| 上高县| 于田县| 三门县| 吉木萨尔县| 和林格尔县| 灵寿县| 乐山市| 富民县| 桦甸市| 启东市| 甘德县| 柯坪县| 精河县| 乡宁县| 孟连| 龙川县| 阜阳市| 大余县| 永和县| 独山县| 诏安县| 教育| 蚌埠市| 海林市| 大荔县| 裕民县| 彭泽县| 东城区| 阿拉尔市|