Cargo Project Init with Proper Structure
Beginner8 minTrending
Initialize a new Rust project with Cargo, set up workspace structure, and configure essential project metadata.
Prerequisites
- -Rust toolchain installed via rustup
Steps
1
Create a new binary project
Initialize a new Rust project with a binary target using Cargo.
$ cargo new myapp && cd myapp
Use 'cargo new --lib mylib' to create a library crate instead of a binary.
2
Set up a workspace for multiple crates
Create a Cargo workspace to manage multiple related crates in one repository.
$ mkdir myproject && cd myproject && cat > Cargo.toml << 'EOF'
[workspace]
members = [
"crates/core",
"crates/cli",
"crates/api",
]
resolver = "2"
EOF
mkdir -p crates && cargo new crates/core --lib && cargo new crates/cli && cargo new crates/api
3
Configure Cargo.toml metadata
Add essential metadata to Cargo.toml for documentation and publishing.
$ cat >> Cargo.toml << 'EOF'
[package]
edition = "2021"
rust-version = "1.75"
description = "A brief description of the project"
license = "MIT OR Apache-2.0"
repository = "https://github.com/yourname/myapp"
keywords = ["cli", "tool"]
categories = ["command-line-utilities"]
EOF
4
Add common dependencies
Use cargo add to install dependencies with proper version constraints.
$ cargo add serde --features derive && cargo add tokio --features full && cargo add anyhow
cargo add automatically picks the latest compatible version and adds it to Cargo.toml.
5
Build and run the project
Verify the setup by building and running the application.
$ cargo build && cargo run
Full Script
FAQ
Discussion
Loading comments...