rust

rust
use anyhow::{anyhow, Result};
use chrono::{DateTime, Utc};
use cron::Schedule as CronSchedule;
use std::str::FromStr;

pub struct Schedule {

Parse Cron Expressions and Run Due Recurring Jobs in Rust with Tokio

rust tokio cron
by codesnips 3 tabs
rust
use wasm_bindgen::prelude::*;

#[wasm_bindgen]
pub fn greet(name: &str) -> String {
    format!("Hello, {}!", name)
}

wasm-bindgen for Rust to JavaScript interop in WebAssembly

rust wasm webassembly
by Marcus Chen 1 tab
rust
use std::sync::Arc;
use std::time::Duration;

use futures::stream::{FuturesUnordered, StreamExt};
use reqwest::Client;
use tokio::sync::Semaphore;

Bounded Concurrent HTTP Fan-Out With FuturesUnordered and Semaphore in Rust

rust async tokio
by codesnips 3 tabs
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 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
use axum::extract::ws::{Message, WebSocket, WebSocketUpgrade};
use axum::extract::State;
use axum::response::Response;
use axum::routing::get;
use axum::Router;

Fan-Out Domain Events to WebSocket Clients With a Tokio Broadcast Channel

tokio async broadcast
by codesnips 3 tabs
rust
use std::time::Instant;

use axum::{
    extract::Request,
    http::HeaderValue,
    middleware::Next,

Axum Latency Logging Middleware With Request IDs and Tracing Spans

axum tower middleware
by codesnips 2 tabs
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