Skip to content

Instantly share code, notes, and snippets.

View matthewjberger's full-sized avatar
🦀
Writing something in Rust, probably

Matthew J. Berger matthewjberger

🦀
Writing something in Rust, probably
  • Hyphen
View GitHub Profile
@matthewjberger
matthewjberger / rust-debug.md
Last active September 7, 2023 16:11
Rust debugging
use serde_json::Value;
fn get_value<'a>(j: &'a Value, p: &str) -> Option<&'a Value> {
let keys = p.split('/').collect::<Vec<&str>>();
let mut current_value = j;
for key in keys {
current_value = match current_value.get(key) {
Some(value) => value,
None => return None,
@matthewjberger
matthewjberger / main.rs
Last active July 21, 2023 05:33
Order of Operations example in rust
fn main() {
let (a, b, c) = (1_i32, 2_i32, 3_f32);
let first_result = (a - b) as f32 / c * 1000.0;
let second_result = (a - b) as f32 / (c * 1000.0);
assert_eq!(first_result, second_result);
}
fd dirname | fzf | cd
@matthewjberger
matthewjberger / main.rs
Last active June 27, 2023 06:11
custom hashmap serialization in rust
use serde::{Deserialize, Serialize};
use serde_json::{json, Value};
use std::collections::HashMap;
#[derive(Debug, PartialEq, Serialize, Deserialize)]
enum UnitEnum {
Variant1,
Variant2,
Variant3,
}
theme = "gruvbox_dark_hard"
[editor]
line-number = "relative"
true-color = true
cursorline = true
gutters = ["diagnostics", "spacer", "line-numbers", "spacer", "diff"]
[keys.normal."space"]
s = ":write-all"
@matthewjberger
matthewjberger / Cargo.toml
Created May 26, 2023 01:14
Put a pixel on the screen with Rust!
[package]
name = "pixel"
version = "0.1.0"
edition = "2021"
[dependencies]
sdl2 = { version = "0.34.5", features = ["bundled"] }
//! Utility for comparing and identifying differences between two structures using serde serialization.
//!
//! The `Diff` struct provides a utility for comparing and identifying differences between two structures in Rust using serde serialization.
//! It allows you to compare two instances of a structure and obtain a list of paths to the differing fields or elements.
use serde::{Deserialize, Serialize};
use serde_json::{Map, Value};
struct Diff;
#[derive(Debug, PartialEq, Eq)]
enum MyEnum {
Variant1(i32),
Variant2(String),
}
fn main() {
let a = MyEnum::Variant1(5);
let b = MyEnum::Variant1(5);
assert_eq!(a.eq(&b), true);
@matthewjberger
matthewjberger / main.rs
Last active July 26, 2024 03:07
Watch a file with debounced file events using notify and tokio in Rust
// Cargo.toml
//
// notify = "5.1.0"
// notify-debouncer-mini = "0.2.1"
// tokio = { version = "1.28.1", features = ["full"] }
use notify::RecursiveMode;
use notify_debouncer_mini::{new_debouncer, DebouncedEventKind};
use std::{error::Error, path::Path, time::Duration};
use tokio::time::sleep;