3

In C world, when building external libraries, their behavior can be significantly customized at compile time by re-defining macros that are put in place by the library author.

If we look at SQLite's Compile-time Options, for example, one can pass the following options to the compiler to effectively change the values of constants defined in the library:

-D SQLITE_DEFAULT_PAGE_SIZE=2097152 -D SQLITE_DEFAULT_SYNCHRONOUS=3

Is this possible to do something similar in Rust? Closest thing I've found is Cargo features, but they only allow you to perform conditional compilation and not substitution of some predefined values at compile-time.

  • Related: https://stackoverflow.com/questions/29871967/what-is-the-difference-between-macros-and-functions-in-rust – jarmod Nov 07 '19 at 00:43
  • 1
    Would something like [this](https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&gist=f3e3f3a43fa5481bc0dcfa3e018fda29) using [this](https://doc.rust-lang.org/reference/conditional-compilation.html#the-cfg-attribute) work? Or perhaps [`env!`](https://doc.rust-lang.org/1.2.0/std/macro.env!.html)? – Optimistic Peach Nov 07 '19 at 00:52
  • 2
    Using `env` would be the more colloquial route I think, but would also involve parsing at runtime, probably with some `lazy_static`, but you could also write a `proc_macro` to parse it instead. – Optimistic Peach Nov 07 '19 at 00:54
  • Related: https://stackoverflow.com/q/37526598 – Arto Bendiken Mar 10 '21 at 13:17

0 Answers0