Dependency of a single file?

i have a file structure:

├── function.rs
└── cargoworkspace
    ├── Cargo.toml
    └── src
        └── main.rs

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

[dependencies.function]
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
[package]
name = "function"
version = "0.1.0"
edition = "2021"

[lib]
path = "function.rs"

With a file layout like this:

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

why not just try

include!("../function.rs");

in your main.rs?

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 = "../../function.rs"]
mod function;

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

thanks anyway!