First timid steps in Rust

  • You are here: Free PHP » Uncategorized » First timid steps in Rust

I'm working on a new site at https://highperformancewebfonts.com/ where I'm doing everything wrong. E.g. using a joke-y client-side-only rendering of articles from .md files (Hello Lizzy.js)

Since there's no static generation, there was no RSS feed. And since someone asked, I decided to add one. But in the spirit of learning-while-doing, I thought I should do the feed generation in Rust—a language I know nothing about.

Here are my first steps in Rust, for posterity. BTW the end result is https://highperformancewebfonts.com/feed.xml

1. Install Rust

The recommended install is via rustup tool. This page https://www.rust-lang.org/tools/install has the instructions:

$ curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh

Next, restart the terminal shell or run:

$ . "$HOME/.cargo/env"

Check if installation was ok:

$ rustc --version
rustc 1.84.0 (9fc6b4312 2025-01-07)

2. A new project

A tool called Cargo seems like the way to go. Looks like it's a package manager, an NPM of Rust:

$ cargo new russel && cd russel

(The name of my program is "russel", from "rusty", from "rust". Yeah, I'll see myself out.)

3. Add dependencies to Cargo.toml

And Cargo.toml looks like a config file similar in spirit to package.json and similar in syntax to a php.ini. Since I'll need to write an RSS feed, a package called rss would be handy.

[package]
name = "russel"
version = "0.1.0"
edition = "2021"

[dependencies]
rss = "2.0.0"

Running $ cargo build after a dependency update seems necessary.

To explore packages, a crates.io site looks appropriate e.g. https://crates.io/crates/rss as well as docs.rs, e.g. https://docs.rs/rss/2.0.11/rss/index.html

4. All ok so far?

The command `cargo new russel` from the previous step created a hello-world program. We can test it by running:

$ cargo run

This should print "Hello, world!"

Nice!

5. Tweaks in src/main.rs

Let's just test we can make changes and see the result. Seeing is believing!

Open src/main.rs, look at this wonderful function:

fn main() {
  println!("Hello, world!");
}

Replace the string with "Bello, world". Save. Run:

$ cargo run

If you see the "Bello", the setup seems to be working. Rejoice!

6. (Optional) install rust-analyzer

It's pretty helpful. In VSCode you can go to Extensions and search for "rust-analyzer" to find it.

Go Rust in peace!

https://en.wikipedia.org/wiki/Rust_in_Peace

Powered by Gewgley