Skip to content

Instantly share code, notes, and snippets.

View BSN4's full-sized avatar
🎯
Focusing

Bader BSN4

🎯
Focusing
View GitHub Profile
@BSN4
BSN4 / collect.rs
Created February 23, 2025 12:45
laravel collection in rust
use std::collections::{HashMap, HashSet};
use std::hash::Hash;
pub struct Collection<'a, T> {
items: Vec<&'a T>,
}
impl<'a, T> Collection<'a, T> {
pub fn new(items: &'a [T]) -> Self {
Self {
@BSN4
BSN4 / rust_async.md
Created January 8, 2025 18:36
Async Rust Practical Patterns and Gotchas

Async Rust Practical Patterns and Gotchas

Struct Patterns

// For shared state across tasks, these are your main tools:
struct MyService {
    // Immutable shared state
    config: Arc<Config>,
    // Mutable shared state
@BSN4
BSN4 / owenership.md
Last active January 8, 2025 18:09
rust non-async ownership

Rust Borrowing and Ownership Rules

Struct Methods

Use &mut self when:

  • Modifying struct fields (e.g., update_score, play_round)
  • Maintaining state between calls

Use &self when:

  • Only reading values (e.g., get_player_move, announce_winner)
@BSN4
BSN4 / async2.rs
Created January 8, 2025 11:17
rust async example
use std::sync::Arc;
use tokio::sync::{broadcast, Mutex};
use tokio::time::{sleep, Duration};
#[derive(Debug)]
struct SharedContext {
counter: i32,
last_updated_by: String,
}
@BSN4
BSN4 / async.rs
Created January 8, 2025 10:37
async rust example
use std::sync::Arc;
use tokio::sync::Mutex;
use tokio::time::{sleep, Duration};
// Our shared context that will be accessed by all workers
#[derive(Debug)]
struct SharedContext {
counter: i32,
last_updated_by: String,
}
@BSN4
BSN4 / rock_paper_scissors2.rs
Created January 2, 2025 01:39
rock_paper_scissors rust v2
use rand::Rng;
use std::io::{self, Write};
#[derive(Debug, Default)]
struct Score {
player: u32,
computer: u32,
}
#[derive(Debug, PartialEq, Clone, Copy)]
@BSN4
BSN4 / rock_paper_scissors.rs
Created January 2, 2025 01:38
rock_paper_rust v1
use rand::Rng;
use std::io::{self, Write};
#[derive(Debug)]
struct Score {
player: u32,
computer: u32,
}
#[derive(Debug, PartialEq, Clone)]
@BSN4
BSN4 / rock_paper_scissors_context.ex
Created November 26, 2024 20:05
rock_paper_scissors_context.ex
defmodule RPS do
defmodule Score, do: defstruct player: 0, computer: 0
defmodule Context do
defstruct [
score: %Score{},
rounds_to_play: 3,
valid_moves: [:rock, :paper, :scissors],
win_conditions: %{rock: :scissors, paper: :rock, scissors: :paper},
io: IO
defmodule RPS do
defmodule Score, do: defstruct player: 0, computer: 0
def play do
%Score{}
|> Stream.iterate(&play_round/1)
|> Enum.at(3)
|> announce_winner()
|> maybe_play_again()
end
@BSN4
BSN4 / gist:bb4663363cbd3f9f4b85d89e3627752d
Created November 26, 2024 18:40 — forked from rxaviers/gist:7360908
Complete list of github markdown emoji markup

People

:bowtie: :bowtie: πŸ˜„ :smile: πŸ˜† :laughing:
😊 :blush: πŸ˜ƒ :smiley: ☺️ :relaxed:
😏 :smirk: 😍 :heart_eyes: 😘 :kissing_heart:
😚 :kissing_closed_eyes: 😳 :flushed: 😌 :relieved:
πŸ˜† :satisfied: 😁 :grin: πŸ˜‰ :wink:
😜 :stuck_out_tongue_winking_eye: 😝 :stuck_out_tongue_closed_eyes: πŸ˜€ :grinning:
πŸ˜— :kissing: πŸ˜™ :kissing_smiling_eyes: πŸ˜› :stuck_out_tongue: