nowfound

Dev tools · December 20, 2022

HT

How to program in Rust as if it was old school C++ with pointers

Basically wrap your data struct in Option>> Better yet, use your custom type that provides few helper functions that make using it as nice as old C++ pointers. Obviously there's no pointer arithmetic, but it works great for dynamically allocated structures like trees or linked lists. Please comment how it's a horrible idea that will bring doom to me and my family. fn main() { struct Data { pub number: i32 } // struct Data { int number; }; let mut p:Pointer = Pointer(None); // Data *p = NULL; println!("{}", p.is_none()); // printf(p == NULL ? "true\n" :…

In plain words

This is a Rust library that wraps data structures in Option<Rc<RefCell<>>> to mimic C++ pointer behavior for developers familiar with that paradigm. It provides helper functions like `is_none()`, `new()`, and `pointed()` to simplify syntax when working with dynamically allocated structures such as trees or linked lists. The tool targets Rust programmers transitioning from C++ who want familiar pointer-like interfaces, though it acknowledges this approach diverges from Rust's safety principles.

written from the facts on this page · September 2026

From the sources

In the maker’s words, at launch

Basically wrap your data struct in Option<Rc<RefCell<_>>> Better yet, use your custom type that provides few helper functions that make using it as nice as old C++ pointers. Obviously there's no pointer arithmetic, but it works great for dynamically allocated structures like trees or linked lists. Please comment how it's a horrible idea that will bring doom to me and my family. fn main() { struct Data { pub number: i32 } &#x2F;&#x2F; struct Data { int number; }; let mut p:Pointer<Data> = Pointer(None); &#x2F;&#x2F; Data *p = NULL; println!("{}", p.is_none()); &#x2F;&#x2F; printf(p == NULL ? "true\n" : "false\n"); p = Pointer::new(Data { number: 5 }); &#x2F;&#x2F; p = new Data { number: 5 }; println!("{}", p.pointed().number); &#x2F;&#x2F; printf("%d\n", p->number); let q = p.clone(); &#x2F;&#x2F; auto q = p; q.pointed().number = 6; &#x2F;&#x2F; q->number = 6; println!("{}", p.pointed().number); &#x2F;&#x2F; printf("%d\n", p->number); println!("{}", p.is_clone_of(&q)); &#x2F;&#x2F; printf(p == q ? "true\n" : "false\n"); } &#x2F;&#x2F; delete p; Helper Pointer<T> type: use std::{rc::Rc, cell::{RefCell, RefMut}}; #[derive(Debug)] struct Pointer<T>(Option<Rc<RefCell<T>>>); impl<T> Clone for Pointer<T> { fn clone(&self) -> Self { Pointer(Some(self.0.as_ref().unwrap().clone())) } } impl<T> Pointer<T> { pub fn new(d: T) -> Pointer<T> { Pointer(Some(Rc::new(RefCell::new(d)))) } pub fn pointed(&self) -> RefMut<T> { let res = self.0.as_ref().unwrap().as_ref().borrow_mut(); res } pub fn is_clone_of(&self, other:&Pointer<T>) -> bool { Rc::ptr_eq(&self.0.as_ref().unwrap(), &other.0.as_ref().unwrap()) } pub fn is_none(&self) -> bool { self.0.is_none() } } Live version (Rust): https:&#x2F;&#x2F;play.rust-lang.org&#x2F;?gist=f43ce73b89537e0a9ae0586169b... Live version (C++): http:&#x2F;&#x2F;tpcg.io&#x2F;_LNG59M

More dev tools this month

the category →
  • Dograh592

    The open source VAPI alternative

    Dev tools · 25d ago · dograh.com

  • Meridian530

    Don't let your work go unnoticed. Get promoted!

    Dev tools · 20d ago · meridiona.com

  • x1516

    Lovable for iPhone apps go from idea to App Store

    Dev tools · 11d ago · x1.new

  • Open-source GTM skills for technical founders

    Dev tools · 29d ago · gtmcofounder.com

  • OpenTrailPaper is open-source bike computer firmware for the LilyGO T5S3 4.7" E-Paper PRO. It supports offline maps, GPX routes, FIT recording and Bluetooth sensors.

    Dev tools · 2d ago · opentrailpaper.com

  • Nuphos380

    The AI-Native DevOps Workspace.

    Dev tools · 24d ago · nuphos.ai

Launched alongside, December 2022

the whole month →