Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

TODO: Crate library and use local library

  • dependencies

  • path

  • Create a library:

cargo new add-lib --lib

The files created:

{% embed include file="src/examples/libraries/add-lib/Cargo.toml)

#![allow(unused)]
fn main() {
pub fn add(left: usize, right: usize) -> usize {
    left + right
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn it_works() {
        let result = add(2, 2);
        assert_eq!(result, 4);
    }
}
}
  • Create another crate that will use this library:
cargo new add-app
cd add-app
cargo add --path ../add-lib/

{% embed include file="src/examples/libraries/add-app/Cargo.toml)

//cargo add --path ../add-lib/

fn main() {
    println!("Using the add_lib crate: {}", add_lib::add(2, 3));
}