rust

rust
extern "C" {
    fn abs(input: i32) -> i32;
}

pub fn safe_abs(input: i32) -> i32 {
    unsafe { abs(input) }

Unsafe Rust for FFI and low-level optimizations

rust unsafe ffi
by Marcus Chen 1 tab
rust
use std::fmt;

#[derive(Debug)]
pub enum HashError {
    Backend(String),
    InvalidHash,

Pluggable Password Hasher Trait in Rust with Argon2 and Bcrypt Backends

rust argon2 bcrypt
by codesnips 3 tabs
rust
use axum::{Router, routing::get};
use tower::ServiceBuilder;
use tower_http::{trace::TraceLayer, timeout::TimeoutLayer};
use std::time::Duration;

async fn handler() -> &'static str {

Tower middleware for composable HTTP service layers

rust tower middleware
by Marcus Chen 1 tab
rust
use std::sync::{Arc, Mutex};
use std::thread;

fn main() {
    let counter = Arc::new(Mutex::new(0));
    let mut handles = vec![];

Arc and Mutex for safe shared mutable state across threads

rust concurrency threading
by Marcus Chen 1 tab
rust
#[derive(Default, Debug)]
struct Config {
    host: String,
    port: u16,
    debug: bool,
}

Default trait for sensible zero values

rust traits
by Marcus Chen 1 tab
toml
[features]
default = ["json"]
json = ["serde_json"]
yaml = ["serde_yaml"]

[dependencies]

Feature flags for conditional compilation

rust cargo features
by Marcus Chen 2 tabs
rust
use serde::{Deserialize, Serialize};

#[derive(Serialize, Deserialize, Debug)]
struct Config {
    host: String,
    port: u16,

serde for zero-copy serialization and deserialization

rust serde serialization
by Marcus Chen 1 tab
rust
use std::any::Any;

pub trait CommandHandler {
    fn name(&self) -> &str;

    fn handle(&self, input: &dyn Any) -> Box<dyn Any>;

Type-Erased Command Handler Registry With Trait Objects in Rust

rust trait-objects dispatch
by codesnips 3 tabs
rust
fn divide(a: i32, b: i32) -> i32 {
    if b == 0 {
        panic!("division by zero");
    }
    a / b
}

panic! and unwinding for unrecoverable errors

rust error-handling
by Marcus Chen 1 tab
rust
use std::cmp::{Ordering, Reverse};

#[derive(Debug, Clone)]
pub struct ScheduledTask<T> {
    pub priority: u8,
    pub seq: u64,

Priority Task Scheduler in Rust with BinaryHeap and Reverse Ordering

rust binary-heap scheduler
by codesnips 3 tabs
rust
use std::collections::HashMap;
use std::sync::{Arc, Mutex, PoisonError};

#[derive(Default)]
struct Metrics {
    per_endpoint: HashMap<String, u64>,

Thread-Safe Request Metrics with Arc, Mutex, and a Poison-Recovery Guard

rust concurrency arc
by codesnips 3 tabs
rust
use nom::{
    bytes::complete::tag,
    character::complete::digit1,
    IResult,
};

nom for parser combinators and zero-copy parsing

rust parsing nom
by Marcus Chen 1 tab