Nono.MA

JANUARY 18, 2024

First, you must install Rust in your machine, which comes with Cargo. (Installing Rust with rustc will also install Cargo.)

Create a new package

cargo new my_package

Build and run your package

cd my_package
cargo build
cargo run

Adding dependencies

Edit Cargo.toml and add your package dependencies.

// Cargo.toml
[dependencies]
base64 = "0.21.7"

You can browse Cargo packages at crates.io.

See how to compile and run Rust programs without Cargo.

JANUARY 17, 2024

Here are two simple Rust programs and how to compile and run them in macOS with rustc.

The simplest Rust program

// first.rs
fn main() {
    println!("Hello, World!");
}

You can compile this program with rustc.

rustc first.rs

Then run it.

./first
# Hello, World!

A program that counts words in files concurrently

I generated this program with ChatGPT and then modified it.

// count.rs
use std::fs::File;
use std::io::{self, BufRead};
use std::path::Path;
use std::thread;

fn count_words_in_file(file_path: &Path) -> io::Result<usize> {
    let file = File::open(file_path)?;
    let reader = io::BufReader::new(file);

    let mut count = 0;
    for line in reader.lines() {
        let line = line?;
        count += line.split_whitespace().count();
    }
    Ok(count)
}

fn main() {
    let file_paths = vec!["file1.txt", "file2.txt", "file3.txt"]; // Replace with actual file paths
    let mut handles = vec![];

    for path in file_paths {
        let path = Path::new(path);
        let handle = thread::spawn(move || {
            match count_words_in_file(path) {
                Ok(count) => println!("{} has {} words.", path.display(), count),
                Err(e) => eprintln!("Error processing file {}: {}", path.display(), e),
            }
        });
        handles.push(handle);
    }

    for handle in handles {
        handle.join().unwrap();
    }
}

We then build and run as before, assuming we have three text files (file1.txt, file2.txt, and file3.txt) in the same directory of our program.

rustc count.rs
./count
# file2.txt has 45 words.
# file3.txt has 1324 words.
# file1.txt has 93980 words.

JANUARY 12, 2024

I got a Permission denied error when trying to cargo install.

› cargo install cargo-wasm
    Updating crates.io index
  Downloaded cargo-wasm v0.4.1
error: failed to download replaced source registry `crates-io`

Caused by:
  failed to create directory `/Users/nono/.cargo/registry/cache/index.crates.io-6f17d22bba15001f`

Caused by:
  Permission denied (os error 13)

This is how I fixed it.

sudo chown -R $(whoami) /Users/nono/.cargo

I then tried to cargo wasm setup and got this error.

› cargo wasm setup
info: syncing channel updates for 'nightly-aarch64-apple-darwin'
error: could not create temp file /Users/nono/.rustup/tmp/628escjb9fzjn4mu_file: Permission denied (os error 13)
Failed to run rustup. Exit code was: 1

Which, again, was solved by changing the owner of ~/.rustup to myself.

sudo chown -R $(whoami) /Users/nono/.rustup

Want to see older publications? Visit the archive.

Listen to Getting Simple .