Use Rust to create the library and call it in exe.

Keywords: Back-end Rust

Use Rust to create the library and call it in exe.

When using cargo new to create a project, there are options for the project type:

  • – bin: compile to executable
  • – lib: compile to library file

The default is – lib. In fact, the types of libraries that Rust can create are as follows:

  • rlib: Rust library, which is the default category of cargo new and can only be called by Rust;
  • dylib: the dynamic link library of Rust specification. It is compiled into. dll on windows and. so on linux. It can only be called by Rust;
  • cdylib: a dynamic link library that meets the C language specification. It is compiled into. dll on windows and. so on linux. It can be called by other languages
  • staticlib: static library. It is compiled into. lib on windows and. a on linux. It can be called by other languages

If it is only called by Rust, the default rlib is the best choice. If you want to use other types, you can't specify cargo directly. You can only modify Cargo.toml manually.

[lib]
crate-type = ["rlib", "dylib", "cdylib", "staticlib"]

cargo allows multiple types to be set at the same time, and the same set of code can be compiled into multiple types at the same time.

Create numrust Library

You can create projects using cargo:

cargo new numrust

Clion can also be used to create files for us. The files created by both are the same:

Create mod in Library

Rust uses the mod keyword to create a namespace. The automatically generated code already contains a mod tests:

#[cfg(test)]
mod tests {
    #[test]
    fn it_works() {
        assert_eq!(2 + 2, 4);
    }
}

Mod is private by default, that is, the tests will not be exported by the library. It is prepared for our testing. We can create our own mod:

pub mod numrust {       // Create a public mod
    pub fn hello() {    // Create public functions
        println!("hello numrust")
    }
}

To call in tests, you need to know the path rules of mod. Rust uses "::" to separate namespaces, and uses three keywords: crite, self and super to represent the starting point of the path:

  • Crite: similar to the root directory
  • self: refers to the current mod, similar to "."
  • super: refers to the parent mod of the current mod, similar to "..."

Numrust and tests are of the same level, so there are two ways to call numrust in tests:

crate::numrust::hello();
super::numrust::hello();

In addition, you can use use use to refer to mod. In Python, people are used to introducing numpy like this:

import numpy as np

This can also be done in Rust:

#[cfg(test)]
mod tests {
    use super::numrust as nr;

    #[test]
    fn it_works() {
        nr::hello();
        assert_eq!(2 + 2, 4);
    }
}

To debug our library, you can directly use the command:

> cargo test
    Finished test [unoptimized + debuginfo] target(s) in 0.01s
     Running unittests (target\debug\deps\numrust-0bda5bdf4b187237.exe)

running 1 test
test tests::it_works ... ok

test result: ok. 1 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out; finished in 0.00s

You can also run in Clion:

Calling Library in exe

Next, create an executable project to call the library just.

cargo new --bin numrustexe

Clion can be used to create:

Then modify Cargo.toml and specify the directory where the library item Cargo.toml is located:

[dependencies]
numrust = { path = "../numrust" }

Then modify main.rs:

extern crate numrust;           // Import external crite
use numrust::numrust as nr;     // Reference numrust, the first is the library name, and the second is the name of mod in the library

fn main() {
    nr::hello();
}

At this point, you can run:

> cargo run
   Compiling numrustexe v0.1.0 (C:\Users\zhangmh\Desktop\MarkDown\numrust\code\numrustexe)
    Finished dev [unoptimized + debuginfo] target(s) in 0.78s
     Running `target\debug\numrustexe.exe`
hello numrust

When the numrust library is modified, cargo will compile numrust first and then numrust exe to ensure that each run is the latest result.

Posted by Deserteye on Wed, 20 Oct 2021 22:36:28 -0700