rust

rust
use tokio::time::{sleep, Duration};

#[tokio::main]
async fn main() {
    let handle = tokio::spawn(async {
        sleep(Duration::from_millis(100)).await;

tokio::spawn for concurrent task execution

rust async tokio
by Marcus Chen 1 tab
rust
use base64::{engine::general_purpose::URL_SAFE_NO_PAD, Engine};
use serde::{Deserialize, Serialize};
use time::OffsetDateTime;
use uuid::Uuid;

#[derive(Debug, Clone, Serialize, Deserialize)]

Cursor-Paginated Streaming REST Endpoint in Axum with Keyset Pagination

axum rust pagination
by codesnips 3 tabs
rust
use serde::{Deserialize, Deserializer};
use time::OffsetDateTime;

#[derive(Debug, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct UserProfile {

Deserializing Optional and Renamed JSON API Fields with Serde in Rust

rust serde serde-json
by codesnips 2 tabs
rust
#[cfg(target_os = "linux")]
fn platform_specific() {
    println!("Running on Linux");
}

#[cfg(target_os = "windows")]

cfg attribute for conditional compilation

rust conditional-compilation
by Marcus Chen 1 tab
rust
#[derive(Debug, Clone, PartialEq)]
struct Point {
    x: i32,
    y: i32,
}

Derive macros for automatic trait implementations

rust macros traits
by Marcus Chen 1 tab
rust
enum Status {
    Ok,
    Error(String),
    Pending,
}

Pattern matching with match for exhaustive case handling

rust pattern-matching
by Marcus Chen 1 tab
rust
use std::collections::HashMap;

fn main() {
    let mut scores = HashMap::new();
    scores.insert("Alice", 10);
    scores.insert("Bob", 20);

HashMap<K, V> for key-value lookups

rust collections
by Marcus Chen 1 tab
rust
use std::fs;
use std::num::ParseIntError;

fn read_port(path: &str) -> Result<u16, Box<dyn std::error::Error>> {
    let contents = fs::read_to_string(path)?;
    let port: u16 = contents.trim().parse()?;

Result and ? operator for clean error propagation

rust error-handling
by Marcus Chen 1 tab
rust
use std::collections::HashMap;
use std::sync::Mutex;

pub type AccountId = u64;
pub type Cents = i64;

Atomic Double-Entry Ledger Transfers With Rust Locking and Poison Recovery

ledger concurrency double-entry
by codesnips 3 tabs
rust
use std::fs;
use std::io::Result;

fn main() -> Result<()> {
    let contents = fs::read_to_string("input.txt")?;
    println!("File contents: {}", contents);

File I/O with std::fs for reading and writing files

rust io files
by Marcus Chen 1 tab
rust
use std::fmt;

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum OrderState {
    Pending,
    Paid,

Type-State Order Lifecycle Machine With Compile-Time Transition Safety in Rust

state-machine enums type-state
by codesnips 3 tabs
rust
use std::fs::File;
use std::io::{BufRead, BufReader, Result};

fn main() -> Result<()> {
    let file = File::open("input.txt")?;
    let reader = BufReader::new(file);

BufReader and BufWriter for efficient I/O buffering

rust io performance
by Marcus Chen 1 tab