rust

rust
use async_trait::async_trait;
use std::io;

#[async_trait]
pub trait ConnectionFactory: Send + Sync + 'static {
    type Connection: Send + 'static;

Building a Bounded Async Database Connection Pool With Tokio Semaphore

rust tokio async
by codesnips 3 tabs
rust
use reqwest;
use serde::Deserialize;

#[derive(Deserialize, Debug)]
struct Post {
    id: u32,

reqwest for async HTTP client with connection pooling

rust http async
by Marcus Chen 1 tab
rust
use thiserror::Error;

#[derive(Error, Debug)]
pub enum ConfigError {
    #[error("file not found: {0}")]
    NotFound(String),

Custom error types with thiserror for domain errors

rust error-handling libraries
by Marcus Chen 1 tab
rust
use std::path::Path;

fn process_file(path: impl AsRef<Path>) {
    let path = path.as_ref();
    println!("Processing: {}", path.display());
}

AsRef and AsMut for flexible function parameters

rust traits conversion
by Marcus Chen 1 tab
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
#[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