Skip to content

Instantly share code, notes, and snippets.

View RandyMcMillan's full-sized avatar
🛰️
Those who know - do not speak of it.

@RandyMcMillan RandyMcMillan

🛰️
Those who know - do not speak of it.
View GitHub Profile
@RandyMcMillan
RandyMcMillan / anyhow_match.rs
Last active September 11, 2025 13:02 — forked from rust-play/playground.rs
anyhow_match.rs
use anyhow::Result;
// The same function as before that might fail.
fn get_user_id(username: &str) -> Result<u32> {
if username.is_empty() {
anyhow::bail!("Username cannot be empty");
}
Ok(42)
}
@RandyMcMillan
RandyMcMillan / anyhow.rs
Created September 11, 2025 12:58 — forked from rust-play/playground.rs
anyhow.rs
use anyhow::Result;
// A function that returns a Result.
// We use 'anyhow::Result' as a shorthand for 'Result<T, anyhow::Error>'.
fn get_user_id(username: &str) -> Result<u32> {
// This is a simple, illustrative error case.
// We can use anyhow::bail! to create an immediate error.
if username.is_empty() {
// 'anyhow::bail!' is a macro that returns an error.
anyhow::bail!("Username cannot be empty");
@RandyMcMillan
RandyMcMillan / struct_partition_into_iter.rs
Last active August 23, 2025 12:01 — forked from rust-play/playground.rs
struct_partition_into_iter.rs
// https://play.rust-lang.org/?version=stable&mode=debug&edition=2024&gist=f6b6cbb5fedbb3a745ee98fde30c8d06
// A basic struct that holds an array of 16-bit unsigned integers.
struct Numbers([u16; 10]);
// Implement the IntoIterator trait for our Numbers struct.
impl<'a> IntoIterator for &'a Numbers {
// We want to iterate over references to u16 values.
type Item = &'a u16;
// The type of the iterator we're returning.
type IntoIter = std::slice::Iter<'a, u16>;
@RandyMcMillan
RandyMcMillan / while_rc_strong_count.rs
Last active August 21, 2025 14:43 — forked from rust-play/playground.rs
while_rc_strong_count.rs
use std::rc::Rc;
fn process_shared(data: Rc<String>, storage: &mut Vec<Rc<String>>) {
storage.push(data.clone()); // Clones Rc, not String
}
fn main() {
let shared = Rc::new("shared".to_string());
let mut storage = Vec::new();
while Rc::strong_count(&shared) < 10 {
process_shared(shared.clone(), &mut storage);
println!("Rc count: {}", Rc::strong_count(&shared));
@RandyMcMillan
RandyMcMillan / impl_from_config_error.rs
Last active August 20, 2025 12:01 — forked from rust-play/playground.rs
impl_from_config_error.rs
#[allow(dead_code)]
#[derive(Debug)]
enum ConfigError {
MissingKey(String),
ParseError(std::num::ParseIntError),
}
impl From<std::num::ParseIntError> for ConfigError {
fn from(err: std::num::ParseIntError) -> Self {
ConfigError::ParseError(err)
}
@RandyMcMillan
RandyMcMillan / tokio_read_write.rs
Last active August 12, 2025 11:53 — forked from rust-play/playground.rs
tokio_read_write.rs
use tokio::io::{AsyncReadExt, AsyncWriteExt};
use tokio::net::TcpListener;
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
let listener = TcpListener::bind("127.0.0.1:8080").await?;
println!("Echo server listening on 127.0.0.1:8080");
loop {
let (mut socket, addr) = listener.accept().await?;
@RandyMcMillan
RandyMcMillan / nested_b_tree.rs
Created August 12, 2025 11:47 — forked from rust-play/playground.rs
nested_b_tree.rs
use std::collections::BTreeMap;
fn main() {
// Let's create a nested B-tree structure:
// A BTreeMap where keys are strings (e.g., "country")
// and values are another BTreeMap (e.g., "city" -> population).
let mut world_populations: BTreeMap<String, BTreeMap<String, u64>> = BTreeMap::new();
// Add some data for "USA"
let mut usa_cities = BTreeMap::new();
usa_cities.insert("New York".to_string(), 8_468_000);
@RandyMcMillan
RandyMcMillan / palindromes.rs
Last active July 29, 2025 16:57 — forked from rust-play/playground.rs
palindromes.rs
fn is_palindrome(n: u64) -> bool {
if n < 10 {
return true; // Single-digit numbers are palindromes
}
let s = n.to_string();
s == s.chars().rev().collect::<String>()
}
fn generate_palindromes_up_to(limit: u64) -> Vec<u64> {
let mut palindromes = Vec::new();
@RandyMcMillan
RandyMcMillan / tokio_echo_server.rs
Last active July 26, 2025 12:01 — forked from rust-play/playground.rs
tokio_echo_server.rs
use tokio::io::{AsyncReadExt, AsyncWriteExt};
use tokio::net::TcpListener;
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
let listener = TcpListener::bind("127.0.0.1:8080").await?;
println!("Echo server listening on 127.0.0.1:8080");
loop {
let (mut socket, addr) = listener.accept().await?;
@RandyMcMillan
RandyMcMillan / rust_to_swift.md
Created July 17, 2025 12:48 — forked from surpher/rust_to_swift.md
Building binaries from Rust to iOS/macOS (PactSwift specific)