Ho

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

Hacker News

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

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 } // struct Data { int number; }; let mut p:Pointer<Data> = Pointer(None); // Data *p = NULL; println!("{}", p.is_none()); // printf(p == NULL ? "true\n" : "false\n"); p = Pointer::new(Data { number: 5 }); // p = new Data { number: 5 }; println!("{}", p.pointed().number); // printf("%d\n", p->number); let q = p.clone(); // auto q = p; q.pointed().number = 6; // q->number = 6; println!("{}", p.pointed().number); // printf("%d\n", p->number); println!("{}", p.is_clone_of(&q)); // printf(p == q ? "true\n" : "false\n"); } // 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://play.rust-lang.org/?gist=f43ce73b89537e0a9ae0586169b... Live version (C++): http://tpcg.io/_LNG59M

Share card

Actual performance

24points
19comments
Made the leaderboard

Launch Intel predictions

Analyze your own launch →
Hacker NewsStrong engagement from HN community · Strong signals: ide, io · Missing: https docs, excited, just released
60%60% predicted probability of success on Hacker News, based on ML models trained on real launch data.
nativeThis product was originally launched on this platform.
Indie HackersFits the IH revenue-focused audience · Missing: supports, reddit linkedin, podcasting
57%57% predicted probability of success on Indie Hackers, based on ML models trained on real launch data.
TrustMRRLess likely to generate early MRR · Missing: mobile apps, ios, personal
49%49% predicted probability of success on TrustMRR, based on ML models trained on real launch data.
Product HuntUnlikely to reach the leaderboard · Strong signals: new, using · Missing: mac, agents, macos
40%40% predicted probability of success on Product Hunt, based on ML models trained on real launch data.
AppSumoMay struggle as an AppSumo deal · Missing: plus, platform, intuitive
40%40% predicted probability of success on AppSumo, based on ML models trained on real launch data.
Acquire.comPre-revenue stage for this audience · Missing: arr, mrr, revenue
19%19% predicted probability of success on Acquire.com, based on ML models trained on real launch data.
BetaListMay not resonate with beta-testers · Missing: web3, chat, crypto
3%3% predicted probability of success on BetaList, based on ML models trained on real launch data.

Correct prediction on native model

Similar products

9S
9S – Basic multithreaded ping program in Rust53%Launch Intel prediction score: how likely this product is to succeed on its source platform, based on its name, tagline, and description.

9S – Basic multithreaded ping program in Rust

Hacker News2
Ho
How to program an Arduino using Rust (on macOS)44%Launch Intel prediction score: how likely this product is to succeed on its source platform, based on its name, tagline, and description.

How to program an Arduino using Rust (on macOS)

Hacker News24
Th
Throwback Machine – Kick it old school51%Launch Intel prediction score: how likely this product is to succeed on its source platform, based on its name, tagline, and description.

Throwback Machine – Kick it old school

Hacker News2
Ol
Old School Graphics Algorithms45%Launch Intel prediction score: how likely this product is to succeed on its source platform, based on its name, tagline, and description.

Old School Graphics Algorithms

Hacker News126
Ol
Old-school Unix talk for the web55%Launch Intel prediction score: how likely this product is to succeed on its source platform, based on its name, tagline, and description.

Old-school Unix talk for the web

Hacker News2
fi
file.st - File Street: The old-school uploader.35%Launch Intel prediction score: how likely this product is to succeed on its source platform, based on its name, tagline, and description.

file.st - File Street: The old-school uploader.

Hacker News15
Fr
Free to play old-school browser MMORPG49%Launch Intel prediction score: how likely this product is to succeed on its source platform, based on its name, tagline, and description.

Free to play old-school browser MMORPG

Hacker News16
Th
Therac-25 Control Program Rewritten in Rust49%Launch Intel prediction score: how likely this product is to succeed on its source platform, based on its name, tagline, and description.

Therac-25 Control Program Rewritten in Rust

Hacker News6
A
A Rust macro that parses Java-like syntax and runs it as a Rust program55%Launch Intel prediction score: how likely this product is to succeed on its source platform, based on its name, tagline, and description.

A Rust macro that parses Java-like syntax and runs it as a Rust program

Hacker News195
Ol
Old-school graphics browser MMORPG49%Launch Intel prediction score: how likely this product is to succeed on its source platform, based on its name, tagline, and description.

Old-school graphics browser MMORPG

Hacker News3