Author: David Fekke
Published: 11/29/2026
Tis’ the time of the year where I awake from my tryptophan induced slumber and start decorating my home for the holidays. I have written in the past how I have over engineered my Xmas lights, but since I am learning Rust, I decided to rewrite the service I use to run my lights in Rust.
I have found one of the best ways to learn a new language is to try using it in practice. This is the way I have learned most of the languages I have used in my career. It usually starts with me reading a book, and then actually writing actual software.
Why am I learning Rust
I have decided to learn Rust for several different reasons. One reason is I believe as a software engineer, you should try to learn a new programming language every year. Even if you don’t use the language, you often learn techniques and concepts you would not have learned if you did not try. One of the other reasons I am learning Rust is because there is a trend right now to refactor or rewrite services and apps in Rust. You might have seen the following meme:

While that meme is funny, there is a lot of truth to the fact that a lot of software is being rewritten in Rust. Anthropic recently rewrote Bun in Rust from Zig. Anthropic is also rewriting most of their services from Python into Rust.
I know a lot of people think that AI will be writing most of the code in the future, so why bother learning Rust. Even if you let AI write Rust code, it certainly does not hurt to be able to read the code that is being generated. And sometimes there is no reason to burn those expensive tokens when you can make a simple change by hand.
The Node.js code
The original service I wrote was pretty simple. It simply used a setInterval timer in the Node runtime to toggle and change the color of my lights every 15 seconds.
import process from 'node:process';
const ip_address = process.env.IP_ADDRESS;
const user = process.env.USERTOKEN;
let toggle = true;
async function setLightStatus(bulb, state) {
const endpoint = `http://${ip_address}/api/${user}/lights/${bulb}/state`;
const options = {
method: "PUT",
body: JSON.stringify(state)
};
await fetch(endpoint, options);
}
async function setLightStatus2() {
let state = {
"on":true,
"sat":254,
"bri":254,
"hue": toggle ? 25600 : 0
};
await setLightStatus(2, state);
}
async function setLightStatus3() {
let state = {
"on":true,
"sat":254,
"bri":254,
"hue": toggle ? 0 : 25600
};
await setLightStatus(3, state);
}
async function masterSwitch() {
await setLightStatus2();
await setLightStatus3();
toggle = !toggle;
}
let timer = setInterval(await masterSwitch, 15000);
process.on('exit', (code) => {
clearTimeout(timer);
console.log(`Program exited with code: ${code}`);
});
If you look at this code, all it is doing flipping a toggle every 15 seconds, and setting either green or red to the bulbs hue. I am using the Philips Hue API to control my bulbs. This API uses a hub that can be accessed over a local area network to remotely control the bulbs through a HTTP request. To find out more about the API and how to talk to Hue bulbs, read my original blog post here.
Why bother rewriting software
In the past I would not have bothered trying to rewrite any software that is working. Most software projects that I have heard about that attempted a full rewrite have failed. Software rewrites have traditionally been difficult to pull off, but I think this is starting to change.
There is a concept in microservices called Replaceable Component Architecture, which means that microservices should be designed to be simple and replaceable. This makes it much easier to replace and rewrite microservices with fewer errors and better performance. The entire microservice can be rewritten and replaced without a huge cost.
The other thing that has helped with rewrites is that LLMs have made it a lot easier to rewrite software from one language to another. Bun was able to be completely rewritten in Rust in a week and half using Claude Code.
Anthropic has also been rewriting their APIs and services in Rust from Python. The main reason why is in the past it was pretty easy to scale applications horizontally over many servers. This is starting to get harder to achieve because of moratoriums on new infrastructure and component costs. As many of you have probably read, politically it is become harder to get new data centers approved. To overcome this challenge, Anthropic decided to start adopting Rust. Rust code can be compiled natively for a specific processor, and tends to be more efficient with memory than using an interpreted runtime like Python.
Rewriting my service in Rust
One of the nice things about Node is you get a lot of features in the runtime for free, like asynchronous code and a built in way to make HTTP requests using the fetch API. JSON is also a native feature in Node.
To use these same features in Rust, you do have to import some cargo modules in order to use these features. For the JSON I used serde. For the HTTP requests I used the reqwest module, and for the asynchronous features I used the Tokio module.
For the body of my PUT request in the service, I created a struct for the body that used a serde attribute to perform the JSON serialization and deserialization.
use serde::{Deserialize, Serialize};
#[derive(Debug, Deserialize, Serialize)]
struct LightState {
on: bool,
sat: i32,
bri: i32,
hue: i32,
}
For reading in the environment variables, I created a function that uses Rust’s Result type for handling any possible errors. The Result type is a common pattern that is also used in languages like Swift for being able to return either a successful response or an error.
fn required_env(name: &str) -> Result<String, Box<dyn Error>> {
let value = env::var(name).map_err(|_| format!("Set the {name} environment variable"))?;
if value.trim().is_empty() {
return Err(format!("The {name} environment variable must not be empty").into());
}
Ok(value)
}
If successful, this should return a String type, but if it isn’t, it will return an Err using a smart pointer.
For the put request we have a function that takes a reqwest client reference, the URL reference and the LightState, and makes a PUT request to my Hue hub.
async fn put_request(
client: &reqwest::Client,
url: &str,
state: &LightState,
) -> Result<(), Box<dyn Error>> {
let response = client
.put(url)
.json(state)
.send()
.await
.map_err(reqwest::Error::without_url)?
.error_for_status()
.map_err(reqwest::Error::without_url)?;
let results: Vec<serde_json::Value> =
response.json().await.map_err(reqwest::Error::without_url)?;
// Hue can return API errors in a successful HTTP response.
for result in &results {
if let Some(error) = result.get("error") {
return Err(format!("Hue API error: {error}").into());
}
}
Ok(())
}
For the main function we have a Tokio attribute for making sure we can make asynchronous calls from the main method. We also use a loop to run the service inside of a run loop. We are using a sleep with a duration of 15 seconds to pause the loop before toggling the bulb colors and looping again. Here is my main function:
#[tokio::main]
async fn main() -> Result<(), Box<dyn Error>> {
let mut toggle: bool = false;
let ip_address = required_env("IP_ADDRESS")?;
let user_token = required_env("USERTOKEN")?;
let client = reqwest::Client::builder()
// The Hue bridge is on the local network and should be reached directly.
.no_proxy()
.connect_timeout(Duration::from_secs(5))
.timeout(Duration::from_secs(10))
.build()?;
println!("Using Hue bridge at {ip_address}");
loop {
toggle = !toggle;
let state1 = LightState {
on: true,
sat: 254,
bri: 254,
hue: if toggle { 25600 } else { 0 },
};
let state2 = LightState {
on: true,
sat: 254,
bri: 254,
hue: if toggle { 0 } else { 25600 },
};
let res1 = put_request(
&client,
&create_endpoint(&ip_address, &user_token, 3),
&state1,
)
.await;
let res2 = put_request(
&client,
&create_endpoint(&ip_address, &user_token, 4),
&state2,
)
.await;
if let Err(e) = &res1 {
eprintln!("Failed to update light 3 on {ip_address}: {e:?}");
}
if let Err(e) = &res2 {
eprintln!("Failed to update light 4 on {ip_address}: {e:?}");
}
tokio::time::sleep(Duration::from_secs(15)).await;
}
}
You can view the whole program on my github repo.
Summary
Did I really need to rewrite my service in Rust? The answer is No. I run my lights off of a small computer in my house. I used to use a Raspberry Pi, but I have upgraded to a Mac that is always on in my office. I have plenty of resources to run this service using Node or Python. If I was still using a Raspberry Pi with limited resources and memory, possibly running many services at the same time, this would have been a worth while experience outside of learning more about Rust.
Node can use as little as 32 to 100 megabytes of RAM to run a typical application, but has been known to go as high as 1.5 gigabytes. The executable for this Rust service only uses 3 megabytes of RAM.
When you are running apps and services in the cloud, you pay for everything. You pay for storage, compute, bandwidth and memory use. Rust can be the tool to help with those costs and scale rapidly without having to scale horizontally.