Introduction

These are running notes on Rust and Axum framework,
featuring learnings from tutorial course @ Youtube by Brooks Builds.

Some useful Extensions for VS Code

Extensions:

  • Even Better TOML
  • crates
  • rust analyzer
  • thunder client
  • rust doc viewer
  • licenser
  • Auto Commit Message

Creating new rust project

Create new project.

cargo init

Add axum dependency (or other library crates)

# This will add axum to the dependencies in Cargo.toml
cargo add axum

Can bring submodule into namespace with

use axum::something;

Note: You don't need to explicitly import top level name of libraries already mentioned in Cargo.toml.
They are available automatically in all modules of your project.


Tip: About local module imports https://stackoverflow.com/questions/59912236/use-module-from-parent-directory-in-rust#comment112271777_59916651

Use crate features

# adding crate
cargo add tokio
# -F is short for feature. we can enable features with the -F flag
cargo add tokio -F rt-multi-thread -F macros

Axum start code: Hello world

#[tokio::main]
async fn main() {
    println!("Hello, world!");
    let app = axum::Router::new().route("/", axum::routing::get(|| async {"hey"}));

    axum::Server::bind(&"127.0.0.1:7999".parse().unwrap())
      .serve(app.into_make_service())
      .await
      .unwrap();
}

Note: see git repo for some steps of code iteration.

Auto build and run on save. (hot reload)

# This is how you *install* rust application to your environment.
# note, not loading a library into project, but installing to system so we can use.
cargo install cargo-watch

Run cargo watch under your project dir

# commands after '-x' will be run whenever a tracked file changes
# tracked meaning not ignored by .gitignore
cargo watch -x run

Build Documentation

# For easy, offline access to docs build them locally
# This will build docs of dependencies also
cargo doc
# open in browser
cargo doc --open

A short note on async/.await

https://rust-lang.github.io/async-book/03_async_await/01_chapter.html

Module

can load local modules with mod statement Then use public functions defined in it

mod server;
use server::run_server;

Modules should be loaded into lib.rs or main.rs. Then in we can load their pub functions in other modules with

use crate::server::runs_server

Also see https://stackoverflow.com/a/70269877

Rust module system summary

Rust Module loading system: 

- Rust module system is hierarchical. 
- lib.rs, and main.rs can be thought of as two separate top level root nodes of the module tree(s)
- the top level node is referred to in `use` syntax as `crate`
- similarly a parent module is referred to as `super` in `use` syntax
- files can't usually load their sibling files (in fs directory structure) as module. 
- Exception to above are mod.rs in a sub directory (aka module), or main.rs, lib.rs in project root directory.
- The above three files can be seen to be one level above other files in the directory, and so can load others.
- Loading module is done using `mod` keyterm. Like `mod little_module`

Serialisation, Deserialisation

We may need to convert string JSON to rust structs or vice versa This can be done using serde library.

cargo add serde
# for derive macros
cargo add serde -F derive

naian is participating in Linux Foundation's LFX 2023 mentorship program, working on the aries-vcx mediator project at Hyperledger Foundation.

Published

Category

Notebook

Tags

Contact