r/programminghorror • • Nov 29 '25

Rust This code is so rusty it gave me tetanus.

Post image
806 Upvotes

r/programminghorror • • Apr 25 '23

Rust Never forget that French people remade RUST in French and called it Rouille (litterally rust in french)

Post image
1.8k Upvotes

r/programminghorror • • Jan 27 '24

Rust Man creates worst parameter type ever, asked to leave

Post image
1.0k Upvotes

r/programminghorror • • Jan 13 '24

Rust detecting chess checks are hard.

Post image
683 Upvotes

r/programminghorror • • Jul 30 '26

Rust [Rust] How to pass references to a trait objects

Post image
133 Upvotes

Later a struct requires pass-by-value generic parameter, but you can't pass trait object (dyn Trait) by value, so some idiot developer (me) decided on this abomination, before realising that that struct can just as easily have the generic parameter passed by mutable reference (adding a + ?Sized).

The rest of the methods are implemented in a similar manner: (**self).method()

r/programminghorror • • Dec 27 '22

Rust Unnecessary shadowing

Post image
434 Upvotes

r/programminghorror • • 11d ago

Rust hello world

37 Upvotes

Yes sorry for stealing your whole concept u/ferro31_ but it really interested me.

r/programminghorror • • Jun 03 '20

Rust 30 minutes later and my code is a fruit salad

Post image
784 Upvotes

r/programminghorror • • Dec 26 '20

Rust Buffalo buffalo Buffalo buffalo buffalo buffalo Buffalo buffalo Buffalo buffalo buffalo.

510 Upvotes

``` mod buffalo { pub(super) struct Buffalo;

impl Buffalo {
    pub(super) fn buffalo<'buffalo>(&self, buffalo: &'buffalo mut Buffalo) -> &'buffalo mut Buffalo {
        buffalo
    }
}

}

fn main() { buffalo::Buffalo.buffalo(&mut buffalo::Buffalo).buffalo(buffalo::Buffalo.buffalo(&mut buffalo::Buffalo)); } ```

r/programminghorror • • Mar 20 '26

Rust Yes i code like this

Thumbnail
gallery
0 Upvotes

r/programminghorror • • Apr 09 '24

rust Seen in a derivatives trading system. Multiplying an enum? Why not? If Low x High = Low, does that mean High = Low/Low = 1?

Post image
310 Upvotes

r/programminghorror • • Feb 11 '24

Rust chess programming is hard.. (piece-square tables)

Post image
250 Upvotes

r/programminghorror • • May 28 '23

Rust I had a dumb idea and somehow it worked

Post image
482 Upvotes

r/programminghorror • • Nov 30 '24

Rust Infringing some of Rust's rules to make a cursed lang where everything is a function... including function declarations

Thumbnail
gallery
108 Upvotes

r/programminghorror • • Aug 29 '22

Rust Best way to read a file to a string

Post image
156 Upvotes

r/programminghorror • • Jan 21 '24

Rust castling is too hard.

Post image
140 Upvotes

r/programminghorror • • Aug 16 '23

Rust The worst where clause in my hobby project

Post image
106 Upvotes

r/programminghorror • • Dec 17 '24

rust Part 2 of my weird "functional" programming language

32 Upvotes

Well, first do ... end blocks allow functions to execute multiple expressions (last value is implicitly returned from a block). Any "variables" and functions declared inside them are going to be fred when end is reached.

Second, "methods" allow a better(?) syntax to call functions on values, without them you'd need to use or a, parse '4' in line 3

Added `do ... end` blocks with their own scopes and "methods"

(parse {str} parses a string to a number because i haven't implemented numeric literals yet, and {a} or {b} acts both as the logical and the bitwise or operator, depending on whether its being ran on bools or numbers)

The way "methods" are implemented is very hacky and imperative (see call_method and the //lit funcs in the rust code).

It essentially parses a or b as a(or, b), and makes a's code be basically like if args.is_empty() { return a; } else { return args[0].eval(a, b); } (where b = args[1]), meaning that a (a()) just returns a, whereas a func b (a(func, b)) returns func(a, b)... Yeah

r/programminghorror • • Oct 09 '24

Rust // make it last an hour

Post image
0 Upvotes

r/programminghorror • • Sep 04 '23

Rust Fleet reformatting 🤦

Post image
95 Upvotes

r/programminghorror • • Nov 04 '22

Rust the HTML5 spec forced me to write this 😭

Post image
45 Upvotes

r/programminghorror • • Aug 07 '22

Rust [Rust] What NOT to do when attempting to avoid allocations.

63 Upvotes

This is what late-night programming gets you.

Use collections, people.

P.S This doesn't work anyway! Expression is a recursive type!

struct Name;

struct Block<N, F, I, A, V, S>
where
    N: Iterator<Item = Name>,
    F: Iterator<Item = Field<N, F, I>>,
    I: Iterator<Item = Expression<N, F, I>>,
    A: Iterator<Item = Attribute>,
    V: Iterator<Item = Variable>,
    S: Iterator<Item = Statement<N, F, I, A, V, S>>,
{
    statements: S,
    retstat: ReturnStatement<N, F, I>,
}

enum ControlIf<N, F, I, A, V, S>
where
    N: Iterator<Item = Name>,
    F: Iterator<Item = Field<N, F, I>>,
    I: Iterator<Item = Expression<N, F, I>>,
    A: Iterator<Item = Attribute>,
    V: Iterator<Item = Variable>,
    S: Iterator<Item = Statement<N, F, I, A, V, S>>,
{
    If(Block<N, F, I, A, V, S>, Expression<N, F, I>),
    ElseIf(Block<N, F, I, A, V, S>, Expression<N, F, I>),
    Else(Block<N, F, I, A, V, S>),
}

enum Statement<N, F, I, A, V, S>
where
    N: Iterator<Item = Name>,
    F: Iterator<Item = Field<N, F, I>>,
    I: Iterator<Item = Expression<N, F, I>>,
    A: Iterator<Item = Attribute>,
    V: Iterator<Item = Variable>,
    S: Iterator<Item = Statement<N, F, I, A, V, S>>,
{
    Assignment(V, I),
    FunctionCall(FunctionCall<N, F, I>),
    Label(Label),
    Break,
    Goto(Name),
    Do(Block<N, F, I, A, V, S>),
    WhileDo(Block<N, F, I, A, V, S>, Expression<N, F, I>),
    Repeat(Block<N, F, I, A, V, S>, Expression<N, F, I>),
    If(ControlIf<N, F, I, A, V, S>),
    For(Name, I),
    ForEach(N, I),
    Function(FunctionName<N>, FunctionBody<N, F, I, A, V, S>),
    LocalFunction(Name, FunctionBody<N, F, I, A, V, S>),
    Local(A, I),
}

struct Attribute {
    name: Name,
}

enum ReturnStatement<N, F, I>
where
    N: Iterator<Item = Name>,
    F: Iterator<Item = Field<N, F, I>>,
    I: Iterator<Item = Expression<N, F, I>>,
{
    None,
    ExpressionList(I),
}

struct ReturnStatment<N, F, I>
where
    N: Iterator<Item = Name>,
    F: Iterator<Item = Field<N, F, I>>,
    I: Iterator<Item = Expression<N, F, I>>,
{
    expressions: I,
}

struct Label {
    name: Name,
}

struct FunctionName<I: Iterator<Item = Name>> {
    names: I,
}

struct Variable;

enum Expression<N, F, I>
where
    N: Iterator<Item = Name>,
    F: Iterator<Item = Field<N, F, I>>,
    I: Iterator<Item = Expression<N, F, I>>,
{
    Nil,
    False,
    True,
    Numeral,
    LiteralString,
    VariableArguments,
    FunctionDefinition,
    PrefixExpression(PrefixExpression<N, F, I>),
    TableConstructor(TableConstructor<N, F, I>),
    BinaryOperation(I), // Self, Self
    UnaryOperation(I),  // Self
}

enum PrefixExpression<N, F, I>
where
    N: Iterator<Item = Name>,
    F: Iterator<Item = Field<N, F, I>>,
    I: Iterator<Item = Expression<N, F, I>>,
{
    Variable(Variable),
    FunctionCall(FunctionCall<N, F, I>),
    Expression(Expression<N, F, I>),
}

enum FunctionCall<N, F, I>
where
    N: Iterator<Item = Name>,
    F: Iterator<Item = Field<N, F, I>>,
    I: Iterator<Item = Expression<N, F, I>>,
{
    Function(PrefixExpression<N, F, I>, Arguments<N, F, I>),
    Method(PrefixExpression<N, F, I>, Name, Arguments<N, F, I>),
}

enum Arguments<N, F, I>
where
    N: Iterator<Item = Name>,
    F: Iterator<Item = Field<N, F, I>>,
    I: Iterator<Item = Expression<N, F, I>>,
{
    ExpressionList(I),
    TableConstructor(TableConstructor<N, F, I>),
    LiteralString,
}

struct FunctionDefinition<N, F, I, A, V, S>
where
    N: Iterator<Item = Name>,
    F: Iterator<Item = Field<N, F, I>>,
    I: Iterator<Item = Expression<N, F, I>>,
    A: Iterator<Item = Attribute>,
    V: Iterator<Item = Variable>,
    S: Iterator<Item = Statement<N, F, I, A, V, S>>,
{
    body: FunctionBody<N, F, I, A, V, S>,
}

struct FunctionBody<N, F, I, A, V, S>
where
    N: Iterator<Item = Name>,
    F: Iterator<Item = Field<N, F, I>>,
    I: Iterator<Item = Expression<N, F, I>>,
    A: Iterator<Item = Attribute>,
    V: Iterator<Item = Variable>,
    S: Iterator<Item = Statement<N, F, I, A, V, S>>,
{
    parameters: ParameterList<N>,
    block: Block<N, F, I, A, V, S>,
}

enum ParameterList<I: Iterator<Item = Name>> {
    Finite(I),
    VariableArguments,
}

struct TableConstructor<N, F, I>
where
    N: Iterator<Item = Name>,
    F: Iterator<Item = Field<N, F, I>>,
    I: Iterator<Item = Expression<N, F, I>>,
{
    fields: F,
}

enum Field<N, F, I>
where
    N: Iterator<Item = Name>,
    F: Iterator<Item = Field<N, F, I>>,
    I: Iterator<Item = Expression<N, F, I>>,
{
    IndexField(Expression<N, F, I>, Expression<N, F, I>),
    NamedField(Name, Expression<N, F, I>),
    NewValue(Expression<N, F, I>),
}

r/programminghorror • • Jan 17 '24

Rust Enum moment

Post image
34 Upvotes

r/programminghorror • • Dec 02 '22

Rust [AoC Day 2 Spoiler] A rather verbose solution. Spoiler

4 Upvotes

I wanted to solve the problems using iterators, but I needed all this to make that work.

A friend of mine called me "long-winded", so I thought it belonged here.

```rust use std::str::FromStr;

use crate::get_input;

type Score = u32; type RoundResult = (Score, Score);

[derive(Debug)]

enum Outcome { Win, Tie, Loss }

impl Outcome { fn score(&self) -> Score { match self { Self::Win => 6, Self::Tie => 3, Self::Loss => 0, } } }

[derive(Debug)]

enum Play { Rock, Paper, Scissors, }

impl Play { fn score(&self) -> Score { match self { Self::Rock => 1, Self::Paper => 2, Self::Scissors => 3, } }

fn play(&self, opp: &Self) -> Outcome {
    match (self, opp) {
        (Self::Rock, Self::Rock) => Outcome::Tie,
        (Self::Rock, Self::Paper) => Outcome::Loss,
        (Self::Rock, Self::Scissors) => Outcome::Win,
        (Self::Paper, Self::Rock) => Outcome::Win,
        (Self::Paper, Self::Paper) => Outcome::Tie,
        (Self::Paper, Self::Scissors) => Outcome::Loss,
        (Self::Scissors, Self::Rock) => Outcome::Loss,
        (Self::Scissors, Self::Paper) => Outcome::Win,
        (Self::Scissors, Self::Scissors) => Outcome::Tie,
    }
}

}

impl FromStr for Play { type Err = ();

fn from_str(s: &str) -> Result<Self, Self::Err> {
    match s {
        // The second matches are from part 1
        "A" | "X" => Ok(Self::Rock),
        "B" | "Y" => Ok(Self::Paper),
        "C" | "Z" => Ok(Self::Scissors),
        _ => Err(())
    }
}

}

[derive(Debug)]

struct Round { opponent: Play, you: Play }

impl Round { fn new(opponent: Play, you: Play) -> Self { Self { opponent, you } }

fn tally(self) -> RoundResult {
    (
        self.opponent.score() + self.opponent.play(&self.you).score(),
        self.you.score() + self.you.play(&self.opponent).score()
    )
}

}

impl FromStr for Round { type Err = ();

fn from_str(s: &str) -> Result<Self, Self::Err> {
    // For part 1
    let mut plays = s.split(" ").map(|s| s.parse::<Play>());
    let opponent = plays.next().transpose()?.unwrap();
    let you = plays.next().transpose()?.unwrap();

    if plays.next().is_some() { return Err(()); }

    Ok(Self::new(opponent, you))
}

}

[test]

fn part1() { let res = get_input("day_2.txt") .lines() .map(|s| s.parse::<Round>().unwrap().tally()) .fold((0, 0), |(o, y), (a, b)| (o + a, y + b)); println!("Opponent: {}, You: {}", res.0, res.1); }

impl FromStr for Outcome { type Err = ();

fn from_str(s: &str) -> Result<Self, Self::Err> {
    // For part 2
    match s {
        "X" => Ok(Self::Loss),
        "Y" => Ok(Self::Tie),
        "Z" => Ok(Self::Win),
        _ => Err(())
    }
}

}

// For part 2 struct Strategy { opponent: Play, outcome: Outcome, }

impl Strategy { fn new(opponent: Play, outcome: Outcome) -> Self { Self { opponent, outcome } } }

impl FromStr for Strategy { type Err = ();

fn from_str(s: &str) -> Result<Self, Self::Err> {
    let mut plays = s.split(" ");
    let opponent = plays.next().ok_or(())?.parse::<Play>()?;
    let outcome = plays.next().ok_or(())?.parse::<Outcome>()?;

    if plays.next().is_some() { return Err(()); }

    Ok(Self::new(opponent, outcome))
}

}

impl From<Strategy> for Round { fn from(strat: Strategy) -> Self { let you = match (&strat.opponent, &strat.outcome) { (Play::Rock, Outcome::Win) => Play::Paper, (Play::Rock, Outcome::Tie) => Play::Rock, (Play::Rock, Outcome::Loss) => Play::Scissors, (Play::Paper, Outcome::Win) => Play::Scissors, (Play::Paper, Outcome::Tie) => Play::Paper, (Play::Paper, Outcome::Loss) => Play::Rock, (Play::Scissors, Outcome::Win) => Play::Rock, (Play::Scissors, Outcome::Tie) => Play::Scissors, (Play::Scissors, Outcome::Loss) => Play::Paper, };

    Self::new(strat.opponent, you)
}

}

[test]

fn part2() { println!("{}", function!(part2)); let res = get_input("day_2.txt") .lines() .map(|s| Round::from(s.parse::<Strategy>().unwrap()).tally()) .fold((0, 0), |(o, y), (a, b)| (o + a, y + b)); println!("Opponent: {}, You: {}", res.0, res.1); } ```

r/programminghorror • • Nov 17 '20

Rust One of mine. Needed to grab the last section of a string path, sans file extension - because who needs Path structs, am I right?

Post image
34 Upvotes