kscos
kscos

Reputation: 23

Rust Crate: Defining configurable variables

If there are certain configurable values in a crate, can they be exposed in some way through a Rust crate?

Like we expose boolean features (yes/no) through it, I would like to expose certain values, say, wait_time (which is an integer) and such.

Upvotes: 0

Views: 312

Answers (1)

cameron1024
cameron1024

Reputation: 10156

One typical way would be with environment variables. Check out eng_logger's use of the RUST_LOG variable, and rayon's use of RAYON_NUM_THREADS as some common examples:

https://docs.rs/env_logger/0.9.0/env_logger/

https://docs.rs/rayon/1.5.1/rayon/index.html

Alternatively, depending on the API you expose to other crates, you might be able to configure it via a typical method call. For example, if your crate has to be "initialized" with a call to some init() function, perhaps that function could take some data as configuration. Perhaps you're passing a configuration struct into function calls already and it could be configured there?

Upvotes: 0

Related Questions