Dependency of a single file?

i have a file structure:

└── cargoworkspace
    ├── Cargo.toml
    └── src

i want to use in cargoworkspace, and tried doing this in Cargo.toml:

path = "../function"

but this did not work. may i know how i can go about using a single source file in a cargo workspace? thank you!

You can't depend on a single file. You can only depend on cargo packages which have a Cargo.toml file.

1 Like

If you want a "single file crate" you do something like this:

// Cargo.toml
name = "function"
version = "0.1.0"
edition = "2021"

path = ""

With a file layout like this:

  ├── Cargo.toml // add a path dependency here to your "function" crate
  ├── function
  │   ├── Cargo.toml // the config above
  │   └──
  └── src
1 Like

why not just try


in your

how about a soft link?

BTW why do you want to use a single file rather than cargo workspace?

because it is a very small function that i feel has no need to come with a Cargo.toml, Cargo.lock, src directory and a .git directory. however, it can be shared across multiple projects.

i tried this, but it didn't work. what worked for me was this:

#[path = "../../"]
mod function;

upon further testing, @erelde 's answer also works.

thanks anyway!

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.