#Cargo examples directory

1 messages · Page 1 of 1 (latest)

kindred bridge
#

When structuring your crate, you can create an examples directory to house various example programs that someone can build / run using the --example X flag. All of the examples I've found are single file examples which rely on the parent crate's Cargo.toml, build.rs, .cargo/config, rust-toolchain.toml, etc. Is there a way to treat the example separately so it can have its own build script, dependencies, etc?

The main one I care about is the build.rs build script. Each example needs to build & link different files (e.g. using a C library that requires the user to select a different implementation of the same file depending on what behavior they want).

#

I tried creating an example crate x under examples/ but using cargo build --example x doesn't find it.

#

The structure here looks something like this (hopefully it copies well)

#

│ .gitignore
│ Cargo.lock
│ Cargo.toml

├───examples
│ ├───a
│ │ │ build.rs
│ │ │ Cargo.toml
│ │ │
│ │ └───src
│ │ c.c
│ │ main.rs
│ │
│ └───b
│ │ build.rs
│ │ Cargo.toml
│ │
│ └───src
│ c.c
main.rs

└───src
main.rs

final parrot
#

possibly in a workspace

#

then cargo run -p x would run crate x

kindred bridge
#

OK, so the current limit is that an example needs to be a single .rs file I guess. I'll check out the workspace idea. Not crazy about having a bunch of examples cluttering up the workspace, but if it works it works.

final parrot
#

I think you can have multi-file examples in the examples folder, but they all have the same set of features, build.rs and so on