← Back to Feed
RLEngine
RLEngine · Level 7473
fun

AI ouroboros

Comments

retoor retoor

He lost me at Rust. What a loser language. Heard about the new unsafe bug? Not that big of an issue, but all that shit and effort for something that can even FAIL. Its guarantees does not even come with guarantees. Please, write C++. Use a small subset.

retoor retoor

Rust version (as in the screenshot):

use std::sync::Mutex;

static KEPT: Mutex<Vec<Box<dyn std::any::Any + Send>>> = Mutex::new(Vec::new());

struct Q<T: 'static + Send>(Option<T>);

impl<T: 'static + Send> Drop for Q<T> {
    fn drop(&mut self) {
        if let Some(t) = self.0.take() {
            KEPT.lock().unwrap().push(Box::new(t));
        }
    }
}

fn main() {
    {
        let l = String::from("hello world, this is long enough");
        let mut q = Q(None);
        q.0 = Some(&l);
    }
    let _z = String::from("XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX");
    let k = KEPT.lock().unwrap();
    println!("{}", k[0].downcast_ref::<&'static String>().unwrap());
}

This compiles and runs on current rustc (1.98). The Drop moves the &String into the global Vec, the next String reuses the same stack slot, and the stored reference observes the new contents. The 'static bound on Q/Drop is not enforced in this inference path (it only fires when the type is forced in a context that demands the bound, e.g. an explicit annotation or a method call from a bounded impl).

C++ version (equivalent idea):

#include <any>
#include <iostream>
#include <mutex>
#include <string>
#include <vector>

std::mutex kept_mutex;
std::vector<std::any> KEPT;

template<typename T>
struct Q {
    std::optional<T> value;

    ~Q() {
        if (value) {
            std::lock_guard lock(kept_mutex);
            KEPT.push_back(std::move(*value));
        }
    }
};

int main() {
    {
        std::string l = "hello world, this is long enough";
        Q<const std::string*> q;
        q.value = &l;
    }
    std::string _z = "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX";
    std::lock_guard lock(kept_mutex);
    auto* p = std::any_cast<const std::string*>(&KEPT[0]);
    std::cout << **p << '\n';
}

(The mutex is kept for fidelity; drop it for a pure single-threaded demo. std::optional is C++17; a raw T* or std::unique_ptr works equally well.)

Comparison

Sense
Rust’s type system is designed to reject exactly this pattern: a non-'static reference must not outlive its referent. The fact that the code type-checks and produces a dangling reference is a surprise (bound checking is incomplete for this inference + Drop path). The resulting behaviour is classic use-after-free in spirit, even though it is still β€œsafe” Rust.

C++ has no such guarantee. Storing a pointer/reference that outlives its object is ordinary undefined behaviour; the language does not try to stop you. The C++ version therefore β€œmakes sense” in the language’s own terms, while the Rust version is a hole relative to Rust’s stated safety model.

Simplicity

  • Rust: more ceremony (Mutex, Box<dyn Any + Send>, explicit 'static + Send bounds, downcast_ref). The bounds are there to express β€œthis value must be able to live forever,” yet they failed to do so.
  • C++: shorter and more direct. std::any (or a simple std::vector<const std::string*>) plus a destructor does the same job with less syntax. No lifetime parameters to get wrong.

The C++ version is simpler to write and read for this particular trick. The Rust version is only β€œsimpler” if you value the (in this case ineffective) static guarantee.

Performance
Both versions do essentially the same work:

  • one heap allocation for the first String/std::string
  • a move of a pointer-sized value into a type-erased container on destruction
  • a second stack allocation that reuses the same slot
  • a lock (or not) + virtual call / any_cast on the read

The string allocations dominate. The extra indirection and type erasure cost is a few nanoseconds and is identical in character on both sides. Removing the mutex (single-threaded) and using a plain std::vector<const std::string*> in C++ makes the C++ path marginally cheaper; the difference is noise compared with the allocations. Neither version is a performance problem for the demonstrated effect.

Summary
The Rust snippet is an interesting demonstration that the 'static bound is not always enforced early enough to prevent a dangling reference from being stored. The C++ translation is shorter, more predictable within C++’s rules, and performs the same. If the goal is a reliable β€œkeep this value alive after the scope ends” pattern, both languages have better idioms (Box::leak / std::unique_ptr + deliberate leak, or an arena). The pattern shown here is mainly useful as a cautionary example.

retoor retoor

If you're not able to write safe code, maybe you should not have a headphone with music on. You just can't handle it. The world is built on that oh so unsafe language. Many people created good stuff with it. So, are you sure it's the language being at fault? Think about it, rusty people.

RLEngine RLEngine

i remember when people used to say i wrote too much text, but now everyone's AI writing and saying nothing

RLEngine RLEngine

know not?

retoor retoor

What do you think about our splash page? I am working on some nice introduction: https://devplace.net/

But i'm not very good at this.

RLEngine RLEngine ↳ @retoor

"busy" page. UI design that makes eyes flow is forgotten

less words is more. i don't want to read things anymore since AI too much "content". nobody reads text anymore either

retoor retoor ↳ @RLEngine

Wanna be a contributor? It is easy, even if you hve no python experience. Just put claude or opencode on it and submit your change. THe workflows and agents are pre-configured making it always the right quality (docs, tests, etc).

D-04got10-01 D-04got10-01

One Rust theory is: "I don't wanna learn [C|C++]. It's too hard! Waaaah, waaaaah!".
/jk

retoor retoor

While Rust is way harder, Rust is for people being able to learn but no discipline. You should net let them cross the road alone without guard rails. I like how every Rust developer is a student telling experienced programmers that it's important. Trust them, they're smart.

Lensflare Lensflare

In for a big surprise πŸ˜…

D-04got10-01 D-04got10-01 ↳ @Lensflare

Am I to understand that Rust is harder than C?

I'd think learning how to properly program, manage memory w/o issues, etc, would be harder.

But, !programmer, so what do I know?

Lensflare Lensflare ↳ @D-04got10-01

Wait, what? You’re not a programmer?

Yeah, I think it depends on how you define "hard" in the context of a programming language.

I’d say that learning Rust is harder than learning C++, and definitely harder than learning C.
It’s more complex. You need to learn how the memory management system works (the borrow checker), the macro system, etc.

C and C++ are easier to learn but it’s harder to write correct programs in them, because you need to remember and know to do things manually, like again, the memory management.

Also, Rust gives some devs a hard-on, so there’s another definition of hard.

D-04got10-01 D-04got10-01 ↳ @Lensflare

Just a simple tester. Thanks for the breakdown. My definition of hard would be the manual management. Since [C|C++] forces you to do that yourself, you have all the power... to do [good|bad]. If you fuck up, you have mem leaks, seg faults && whatnot. Sounds harder to me.

Lensflare Lensflare ↳ @D-04got10-01

For a !programmer, you use a lot of programmer syntax πŸ˜…

D-04got10-01 D-04got10-01 ↳ @Lensflare

return true;

retoor retoor ↳ @Lensflare

C++ is actually easier if you just restrict to the basics (or subset) and then in general it's already the end of memory issues. C++ comes with a lot of BS and a true programmer sees that and skips it for the same reason as Rust.

But also, Rust is what you want as a beginning coder, complex shit that makes you feel smart. It takes real effort to learn. Well, React too actually. It's a surprise that so many people do it. Yeah, they feel skilled because they can do shit. But I always say, if you're past the half of a react tutorial and did not zone out, I have bad news for ya. I mean it. ExtJS was considered extremely hard to learn and nobody wanted back then. It was not normal back then to take js seriously, writing more than a validation and for datepickers you had mootools. It was jquery era. But ExtJS would even be modern today, the version of back then. But they switched to react base πŸ˜’. Yeah, commercial company selling 2k-5k Java script licenses.. What did you expect.

retoor retoor ↳ @D-04got10-01

You're putting yourself too low do I downvoted you. Ty should not speak bad about themselves or expressing doubt.

Rust is like a factor 5 times harder than C. C requires one thing more than php and some people just can't. Literally can't.

You also have two type of people: that person that can explain you pointers in a hour or that guy that has a three hour video about pointers. You would think the latter must be a genius. I actually think it's very sad πŸ˜‚πŸ‘†.

D-04got10-01 D-04got10-01 ↳ @retoor

LOL. '-1'. Never noticed until now. No clue who downvoted.

retoor retoor ↳ @D-04got10-01

I did, I told ya. Not even d4gottii may downplay d4gottii. You cut yourself? I'll downvote five posts!

D-04got10-01 D-04got10-01 ↳ @retoor

Your comment was slightly incoherent. Couldn't really understand the point you were trying to make.

retoor retoor ↳ @D-04got10-01

That's a bit normie comment but it's OK 😁