[Solved] Multiple binaries: How to access common code?

I'm trying to create 2 binaries with some common code. How do I access a function in my common code?
Here's my project:

// src/lib.rs
pub fn add(a: i32, b: i32) -> i32 {
    a + b
}
// src/bin/a.rs
// How do I import add()? <-- MY PROBLEM
fn main() {
    println!("{}", add(1, 2));
}
// src/bin/b.rs
<snip>
# Cargo.toml
cargo-features = ["edition"]

[package]
<snip>
edition = "2018"

[dependencies]

FWIW, I'm on nightly, using the 2018 edition. If I need to restructure my project, how?

You snipped the relevant part out of your Cargo.toml, but you can import add with a use statement:

use YOUR_CRATE_NAME::add;
1 Like

Thanks a bunch! Seems to work. :slight_smile:

(Just documenting my next change:)
When moving the function to a module, this seems to work, given crate name "rust-example":

// src/lib.rs
pub mod math;
// src/math.rs
pub fn add(a: i32, b: i32) -> i32 {
    a + b
}
// src/bin/a.rs
use rust_example::math::add;

fn main() {
    println!("{}", add(1, 2));
}