Skip to content

Latest commit

 

History

History
121 lines (92 loc) · 3.18 KB

UsingRustTMSN.md

File metadata and controls

121 lines (92 loc) · 3.18 KB

Using rust_tmsn in your projects

To use rust_tmsn in your own project, please download this repository to your computer, and append following lines to the Cargo.toml file in your project where the path should be the actual location of where this repository is downloaded.

[dependencies]
rust_tmsn = { path = "../rust-tmsn" }

Usage

  1. Clone this project to your computer
git clone https://github.com/arapat/rust-tmsn.git
  1. In the same directory, create your own project
cargo new my_project

Now you should see two directories in your workspace

$ ls
my_project rust-tmsn
  1. Append the dependency to rust-tmsn in your project

Edit the Cargo.toml file. After the editing, your should see the following

$ cat my_project/Cargo.toml
[package]
name = "my_project"
version = "0.1.0"
authors = ["Your Name <[email protected]>"]

[dependencies]
rust_tmsn = { path = "../rust-tmsn" }
  1. Write your program in my_project/src/main.rs. For example, you can copy and paste the content of the example network.rs we provided.

  2. (Optional) To print the runtime logs to the terminal, you should use the env_logger package in your project code.

First add the dependency to the env_logger in the Cargo.toml file.

[dependencies]
env_logger = "0.5.5"
time = "0.1.39"

The time crate is optional, and used for specifying the logging format (see below).

Then specify the dependency in the project code by adding the following line in the very begining of src/main.rs

extern crate env_logger;
extern crate time;  // optional

use time::get_time; // optional

Finally, define the logging format in the definition of the main() function, for example

fn main() {
    // get_time() function requires the `time` crate dependency
    let base_timestamp = get_time().sec;
    env_logger::Builder
              ::from_default_env()
              .format(move|buf, record| {
                  let timestamp = get_time();
                  let epoch_since_apr18: i64 = timestamp.sec - base_timestamp;
                  let formatted_ts = format!("{}.{:09}", epoch_since_apr18, timestamp.nsec);
                  writeln!(
                      buf, "{}, {}, {}, {}",
                      record.level(), formatted_ts, record.module_path().unwrap(), record.args()
                  )
              })
              .init();
    // ... more code
}

Please refer to the find-prime-nums.rs file under the /examples directory for a more specific example of enabling the logging.

Note that env_logger is used for configuration so that the logs would be written to stdout/stderr (which can then be redirected to other files using the commandline arguments if you like). To use the actual logging APIs (e.g. info!(), error!(), etc.), one should use the log crate. For more information on using logs in Rust, please check out the documentation of the log crate.

  1. Compile your program and run it
cd my_project

# Compile
cargo build  # without optimization
cargo build --release  # with optimization

# Run
cargo run  # without printing logs
RUST_LOG=debug cargo run  # with printing logs