How to Create an Own Crate
Crate a workspace
mkdir workpsaces && cd workpsaces
mkdir hello-world && cd hello-world
touch Cargo.toml
echo '[workspace]
members = ["lib-hello", "bin-hello", "bin-local-hello", "lib-extern"]' >> Cargo.toml
I. develop the crate
Step 1: create the default crate
mkdir lib-hello && cd lib-hello
cargo init --name hello_exercism --lib
Step 2: develop the crate source and test codes for folder 'tests'
vi Cargo.toml
vi src/lib.rs
mkdir tests
touch tests/u_hello.rs
vi tests/u_hello.rs
touch tests/i_hello.rs
vi tests/i_hello.rs
cargo test
Step 3: develop the example codes
mkdir examples
touch examples/u_hello.rs
vi examples/u_hello.rs
cargo run --example u_hello
touch examples/i_hello.rs
vi examples/i_hello.rs
cargo run --example i_hello
Step 4: develop the crate source and test codes for folder 'src'
vi src/lib.rs
mkdir -p src/integration_tests
touch src/integration_tests/mod.rs
vi src/integration_tests/mod.rs
touch src/integration_tests/i_hello.rs
vi src/integration_tests/i_hello.rs
mkdir -p src/private_tests
touch src/private_tests/mod.rs
vi src/private_tests/mod.rs
touch src/private_tests/owned_hello.rs
vi src/private_tests/owned_hello.rs
cargo test
publish the own crate to crates.io
cargo login <token>
cargo test
cargo package
cargo publish
II. use the crate 'hello_exercism'
Step 1: create the default Bin
mkdir bin-hello && cd bin-hello
cargo init --name bin-hello --bin
Step 2: configure the file Cargo.toml
echo 'hello_exercism = "0.4.0"' >> Cargo.toml
Step 3: edit the rust file main.rs
use hello_exercism;
fn main () {
println!("{}",hello_exercism::hello());
assert_eq!("Hello, World!", hello_exercism::hello());
}
Step 4: run the Bin program
cargo run main
III. create the crate 'hello_exercism' doc in local version
cargo doc --open --package hello_exercism
IV. create the crate 'hello_exercism' doc in server version
- github.com >> >> Setting >> Options >> GitHub Pages >> (INFO...)
- Go to Crate Root Path
mkdir <REPOSITORY>/docs/<PROJECT_NAME>
cargo doc
cp -rf target/doc/. <REPOSITORY>/docs/<PROJECT_NAME>/.
- Example:
- Go to Crate Root Path
mkdir -p ../../docs/hello-world
cargo doc
cp -rf target/doc/. ../../docs/hello-world/