Skip to content

Instantly share code, notes, and snippets.

View DrPlantabyte's full-sized avatar

Chris Hall DrPlantabyte

  • Sydney, Australia
View GitHub Profile
@DrPlantabyte
DrPlantabyte / build.rs
Last active October 15, 2022 21:16
build.rs for automatic version increment on build
use std::error::Error;
// NOTE: requires `regex = "1.6"` under [build-dependencies] in Cargo.toml
fn main() {
println!("Running build.rs...");
match run() {
Ok(_) => println!("...Success!"),
Err(e) => {
println!("...Failure!");
eprintln!("{:?}", e);
@DrPlantabyte
DrPlantabyte / better_errors.rs
Created July 3, 2023 02:32
HOW TO IMPLEMENT JAVA-STYLE EXCEPTIONS IN IDIOMATIC RUST
/*
The contents of this file are Public Domain. You may use as you wish.
HOW TO IMPLEMENT JAVA-STYLE EXCEPTIONS IN IDIOMATIC RUST
(aka "How to have errors that don't suck")
Here we define a macro that creates a struct implementing the Error trait and automatically captures
and displays the backtrace unless running in release mode (and even then, you can specify
`RUST_BACKTRACE=1` to get the backtraces in release)
use std::sync::{Arc, Mutex};
use tokio;
fn main() -> Result<(), Box<dyn std::error::Error>> {
println!("Starting...");
println!("===== Direct Execution ===");
println!("Got {} from Task 1", task1());
println!("Got {} from Task 2", task2());
println!("Got {} from Task 3", task3());