4 May 2022

Getting Started with Rust

I’m learning Rust by updating a library, this is tricky.

Resources to get started

Basics: https://doc.rust-lang.org/rust-by-example/mod/use.html

Async Programming: https://rust-lang.github.io/async-book/01_getting_started/02_why_async.html

Notes on Async

Rust async execution is explicit and make sense: futures are inert, they don’t run if you’re not waiting for their result. Much different from JS for example: I never found a precise description of what happen to an async that is not await’d for.

Open Questions

What is the ? operator used in some_call().await?

It’s a way to rethrow exceptions, see: https://doc.rust-lang.org/nightly/core/result/index.html#the-question-mark-operator-

What is the dyn parameter used in return value?

Example:

async fn main() -> Result<(), Box<dyn std::error::Error>> {
    let (client, _run_parameters) = testground::client::Client::new().await?;

    client.record_success().await?;

    // init();
    Ok(())
}

What is the equivalent of a js / python lambda function?

They call it closures. Makes sense because they are capturing their env,

https://riptutorial.com/rust/example/5933/passing-lambdas-around https://sodocumentation.net/rust/topic/1815/closures-and-lambda-expressions

Doing async in these doesn’t work straight out of the box.

How can you inspect macros & decorators?

I couldn’t find what #[clap(env)] does, googling for clap, I get something about command line parsing: https://docs.rs/clap/latest/clap/

I guess this is loading data from the env, but I couldn’t find explicit documentation immediately in VSCode.

use clap::Parser;

use std::path::PathBuf;

use ipnetwork::IpNetwork;

#[derive(Parser, Debug, Clone)]
pub struct RunParameters {
    #[clap(env)]
    pub test_plan: String, // TEST_PLAN: streaming_test
    #[clap(env)]
    pub test_case: String, // TEST_CASE: quickstart

Laurent Senta

I wrote software for large distributed systems, web applications, and even robots. Now I focus on decentralization, overly-engineered software, and frugal innovation. I help companies around the world build products through SingularGarden.