Learn
Services
Features

Features

A Service will likely depend on external libraries that might be heavy to load and unecessary to some client. For example, if you have a web server and an app sharing some logic, you might want to import sqlx only in the web server and use some in-memory caching in the app.

To do so, you can either split your services into different crates or use the features attribute in your Cargo.toml:

services/Cargo.toml.rs
[dependencies]
sqlx = { version = "1", optional = true }
 
[features]
pgstore = ["dep:sqlx"]
services/src/lib.rs
#[cfg(feature = "pgstore")]
pub mod pgstore {
    use sqlx::PgPool;
    // ...
}
client/server/Cargo.toml.rs
[dependencies]
service = { path = "../../services", features = ["pgstore"] }