nxe
nxe

Reputation: 261

Adding a custom value into a Cargo.toml file

Background

I'm currently in the process of writing a crate that's bindings for a C library and I need the user to specify where the built library is. Previously, I've seen this handles by the llvm-sys crate using environment variables. However, having used this a lot it can become quite cumbersome to have to write this every time I want to run a project.

similar questions

I found this post that asked a similar thing, but seemed to only be able to use specified tags, so not able to specify a path.

What I want to do

I would like to know if there's any way I can have a specific custom value in the Cargo.toml file. The idea being the crate I'm writing would require a specific value (I'll call it example here) that can take any string value. so when this crate were to be used in another project, that projects Cargo.toml file would be similar to this::

[package]
name = "some-other-project"
version = "0.1.0"
edition = "2021"

[dependencies.my_crate]
version = "0.1.0"
example = "something goes here"

This value would then be accessible by the crate I'm writing (the dependency in regards to the above manifest file) and usable in some way.

Upvotes: 3

Views: 4216

Answers (1)

Chayim Friedman
Chayim Friedman

Reputation: 70900

You can set environment variables using a Cargo configration file (.cargo/config.toml) then you can retrieve them in downstream crates by using e.g. env!()/option_env!()/build scripts.

Upvotes: 1

Related Questions