Documentation
Generating Documentation with cargo doc
The cargo doc command invokes rustdoc on your package (and optionally its dependencies) to compile all /// and //! documentation comments into a static, offline-searchable HTML website inside target/doc/.
Source Files (`src/lib.rs`, `Cargo.toml`)
|
v `cargo doc --no-deps --open`
[ rustdoc compiler ]
|
v
target/doc/<crate_name>/index.html (Static HTML + JS Search Index)
Essential cargo doc Flags
| Command | Behavior |
|---|---|
cargo doc |
Generates HTML docs for the current package and all of its dependencies in target/doc/. |
cargo doc --no-deps |
Generates HTML docs only for the current package (or workspace), skipping dependency docs for much faster builds. |
cargo doc --open |
Generates docs and automatically opens the crate's root index.html in your default web browser. |
cargo doc --document-private-items |
Includes private functions, private struct fields, and internal modules in the generated HTML (useful for internal application architecture docs). |
cargo doc --workspace --no-deps |
Builds unified documentation for all crates in a Cargo workspace. |
cargo doc --all-features |
Enables all Cargo feature flags so feature-gated modules and items are included in the docs. |
# Fast local iteration workflow: build only local crate docs and open in browser
cargo doc --no-deps --open
Controlling rustdoc Behavior with Crate Attributes
Crate-level #![doc(...)] attributes at the top of src/lib.rs customize how rustdoc renders your crate and enforces documentation coverage:
| Crate Attribute | Effect |
|---|---|
#![warn(missing_docs)] |
Emits a compiler warning on any public item (pub struct, pub fn, pub enum variant, pub field) that lacks a doc comment. |
#![deny(rustdoc::broken_intra_doc_links)] |
Fails cargo doc if any [Item] intra-doc link cannot be resolved. |
#![deny(rustdoc::bare_urls)] |
Warns/errors on unlinked raw https://... URLs not wrapped in <https://...> or [text](url). |
#[doc(hidden)] |
Hides a pub item (such as an internal macro helper) from generated HTML docs while keeping it publicly exported for macros. |
#[doc(alias = "search_term")] |
Adds alternative search keywords (such as operator symbols "+" or terminology from other languages) to rustdoc's search bar. |
#[doc(inline)] / #[doc(no_inline)] |
Controls whether a pub use re-export is rendered inline on the current page or listed under a "Re-exports" link. |
#![warn(missing_docs)]
#![deny(rustdoc::broken_intra_doc_links)]
//! Core string joining utilities.
/// Joins two strings with a `/` separator.
///
/// Users searching for `"concat"` or `"path_join"` in `cargo doc` will find this function.
#[doc(alias = "concat")]
#[doc(alias = "path_join")]
pub fn join_slash(left: &str, right: &str) -> String {
format!("{}/{}", left.trim_end_matches('/'), right.trim_start_matches('/'))
}
/// Internal helper exported only for declarative macros; hidden from `cargo doc`.
#[doc(hidden)]
pub fn __internal_macro_helper() -> &'static str {
"internal"
}
fn main() {
println!("Joined: {}", join_slash("/api/v1/", "/users"));
}
Joined: /api/v1/users