r/learnrust • • Jan 11 '24

Why does Rust produce inconsistent results?

0 Upvotes

So, for the first time in a decade of programming in various languages, I'm getting intermittently wrong results from execution. Specifically, I run the same non-network program with the same input and get different results.

I plan to hunt this down, paring the example down, etc. Really, I'm trying to understand why Rust allows this? I thought consistency was part of it's zen.

The core data structures are a cyclic tree of nodes, with each node having a three character identifier, and identifiers for the left and right:

type NodeList = HashMap<String, (String, String)>

Also, I have a mutating vector of positions for simultaneously walking the nodes. That is, for each item in the vector, change it's string pointer to point to the corresponding node table's left or right entry. The suspicious code for this:

let mut positions : Vec<&str> = ....
...
for pos in &mut positions {

let p = next_position(pos, lr, &nodes); *pos = p; }

When I run with the same data, I get different results.

  • Why?

  • Why does Rust allow it to compile?

The full code

https://gist.github.com/merriam/d5f63bc6cb279812396006ca1010fe6a

The output

General output with lots of debug information:

One version ends with

After 2 steps, next char is L, positions are ["11Z", "22C"]

After 4 steps, next char is L, positions are ["11Z", "22B"]

Final positions are ["11Z", "22Z"]

Ran for after 6 steps.

or sometimes

After 3 steps, next char is R, positions are ["22Z", "11B"]

Final positions are ["22Z", "11Z"]

Ran for after 6 steps.

The input

https://gist.github.com/merriam/23a4ce77889436d09966423b3429f6d5

LR

11A = (11B, XXX)

11B = (XXX, 11Z) 11Z = (11B, XXX) 22A = (22B, XXX) 22B = (22C, 22C) 22C = (22Z, 22Z) 22Z = (22B, 22B) XXX = (XXX, XXX)


r/learnrust • • Aug 02 '26

Help Learning Rust From Scratch

1 Upvotes

Hello ! after a while of debating and self reflection, i have decided to start learning rust, from scratch (I am genuinely interested and this is an actual question from my end) (I hope you guys can help develop a fellow rustacean :) ). Now im a programming newbie, barely learned python basics, and in college rn, but i have picked up a sudden interest in rust. Suggest me an exact roadmap of how you would learn rust (if you are a rust pro then i highly appreciate your advice, and even if u are a newbie it would be nice to discuss how learning it is like). Just so you know, i havent started at all yet, fresh slate. I will try my best to reply to every comment


r/learnrust • • May 27 '25

Rust so far

2 Upvotes

After migrating to rust and reading every corner of the rust book . finally I am making my own http server from scratch. God bless me .


r/learnrust • • Jul 04 '24

For a Non IT professional, how tough is learning Rust programming? Thanks!!

0 Upvotes

r/learnrust • • Dec 07 '25

I built A-Lang in Rust — a small language for learning and experimentation

2 Upvotes

Hi r/learnrust,

I’ve been building A-Lang, a small programming language written in Rust, inspired by Lua’s simplicity and Rust’s readability.
It’s designed to be lightweight, fast, and embeddable — great for scripting, tools, and small game engines.

Goals:
• Minimal, clean syntax
• Fast compilation and startup
• Static/dynamic typing (simple but practical)
• Small runtime, easy to embed

GitHub: https://github.com/A-The-Programming-Language/a-lang

I’d love to hear from learners and Rust enthusiasts:

  • What parts of the language or compiler design are easiest to understand?

  • Any tips or suggestions for making A-Lang more beginner-friendly?

  • Features you’d love to see in a small Rust-based language project

Feedback and ideas are very welcome — especially from people learning Rust!


r/learnrust • • Feb 23 '26

Neural Network for Rust AI

Thumbnail
github.com
0 Upvotes

Hi guy, sharing with you some framework I've working on since a long time and wanna make it publish as carte for having it own files (.rnn) for AI's neural Network. It work like ".pt" with python but native code no_std for rust

Hi've also create a codespace to get a thrustfull code source, and already make a copilot on the amelioration of the new crate.

Any looks, contributions or modification to optimize it is welcome !

Here my crates.io account

Had already some upload and review by user :

https://crates.io/users/rayanmorel4498-ai

so any advise to make it work !


r/learnrust • • Dec 23 '21

Can I iterate through the key-value pairs of a JSON object with Serde(/anything-else)?

0 Upvotes

I want to iterate through JSON objects, without making their structures.

I didn't really found any similar code in the documentation of Serde. Maybe you can recommend an other library for that.


r/learnrust • • Nov 02 '20

Rustup - do I *have* to? Why??

8 Upvotes

TL;DR: why do I need rustup? Not going to use it unless there's a compelling reason.

Not enthused in the least to use rustup. I won't install something by CURLing a shell script from the Internet that creates a bunch of dotfiles in my profile. It's a massive security faux pas (I honestly can't believe devs haven't laughed this out of the room yet), I dont like that everything is installed only for my local user, seems like an unnecessary wrapper and something else to break, etc.

Edit: for those who find this in a google search, here's the answer:

Several Linux distributions package Rust, and you may wish to use the packaged toolchain, such as for distribution package development. You may also wish to use a rustup-managed toolchain such as nightly or beta. Normally, rustup will complain that you already have Rust installed in /usr and refuse to install. However, you can install Rust via rustup and have it coexist with your distribution's packaged Rust.

When you initially install Rust with rustup, pass the -y option to make it ignore the packaged Rust toolchain and install a rustup-managed toolchain into ~/.cargo/bin. Add that directory to your $PATH (or let rustup do it for you by not passing --no-modify-path). Then, to tell rustup about your system toolchain, run:

rustup toolchain link system /usr

You can then use "system" as a rustup toolchain, just like "nightly". For example, using the toolchain override shorthand, you can run cargo +system build to build with the system toolchain, or cargo +nightly build to build with nightly.

If you do distribution Rust development, you should likely make "system" your default toolchain:

rustup default system

r/learnrust • • Dec 26 '22

Learning rust

0 Upvotes

I just started learning @golang and @rustlang recently and will be posting my learning on Twitter everyday so that I can be accountable and might help someone in the process. Let's go and be rusty

https://www.youtube.com/channel/UC-wcneYz7rKa-fWg7PJaM0A

https://twitter.com/BraveEvidence


r/learnrust • • Nov 23 '23

If I really really want to, can I disable the borrow checker?

0 Upvotes

I want to turn off the borrow checker so I can tell people ‘I tried it, it was bad, don’t do it.’ I’m taking one for the team. How do I turn off the borrow checker?


r/learnrust • • Jul 27 '25

Resource to learn rust

0 Upvotes

I am new to rust and i want to learn the internals or it from the beginning so i will not struggle in the future without knowing it. I am interested in web back-end frameworks. I searched in the youtube and found a course by free code camp. But it didn't cover async and other stuff. Can anyone suggest me a good resource? Thanks in advance!!

Edit: Also suggest me resources to learn tokio and actix.


r/learnrust • • May 15 '24

Rust Borrow Checker

0 Upvotes

Why does one work and the other dosen't, technically the print macro should print out in both cases


r/learnrust • • Sep 25 '26

I built a Rust project that turns `effects(pure)` into a real transitive compiler obligation — curious what you think

0 Upvotes

Hey r/learnrust,

I’ve been working on Nirdosha (still pre-1.0), a project that tries to give you plain Rust with compiler-checked contracts. The part I care about most isn’t the syntax itself — it’s making a claim like this actually mean something across the whole call graph:

#[nirdosha_rt::contract(effects(pure))]
fn net_total(records: &[u64]) -> u64 {
    records.iter().sum::<u64>() + ledger_overrides()
}

Looks pure at first glance. But the chain continues:

fn ledger_overrides() -> u64 {
    read_ledger().len() as u64
}

fn read_ledger() -> String {
    std::fs::read_to_string("ledger.txt").unwrap_or_default()
}

A local AST scan would happily accept net_total. In deep verification mode, Nirdosha drives rustc, inspects pre-optimization MIR, resolves the call graph, and rejects the build with a diagnostic like:

fn `net_total` claims effects(pure) but the effect lattice says otherwise
file system access — chain:
net_total
→ ledger_overrides
→ read_ledger
→ std::fs::read_to_string

A few design choices that felt important to me:

  • Calls are identified through resolved DefIds, not string matching.

  • Local callees are analyzed transitively.

  • Closures (including ones passed to Iterator::map) get inspected so effects can’t hide inside callbacks.

  • MIR is checked before optimization, so the compiler can’t erase a forbidden operation before verification sees it.

  • File, network, process, environment, clock, randomness, FFI, panic, unsafe, and mutable-static boundaries are all treated as effects.

  • Dynamically dispatched or unresolved calls fail closed.

  • External functions need a curated effect summary — unknowns are never silently assumed pure.

  • Recursion is handled by tracking visited functions.

  • Potential panics (bounds checks, division by zero, etc.) count as impurity/partiality.

The two-level design is deliberate. Under ordinary stable cargo the attribute macro does a shallow check and expands to normal Rust. Under cargo nirdosha build a nightly rustc-dev driver runs the full interprocedural MIR analysis. There’s also an explicit fast mode that only does the shallow check — I don’t pretend they provide the same guarantee.

I applied the same philosophy to authorization:

#[nirdosha_rt::contract(requires(role = "hr_staff"))]
fn compute_payroll(records: &[PayRecord]) -> u64 { ... }

The macro injects &RoleProof<HrStaff> into the real signature. RoleProof has a private constructor and can only be minted after a proper authenticated session, so forgetting the check becomes a type error at the call site instead of a runtime branch someone might skip.

The enforcement ladder I’m aiming for is:

  1. Express the requirement next to ordinary Rust code.

  2. Encode whatever can be enforced directly into the type system.

  3. Catch obvious violations during macro expansion.

  4. Use resolved MIR and the transitive call graph for deeper claims.

  5. Produce a certificate describing exactly which guarantees were checked.

Effect summaries form a trusted boundary, dynamic dispatch is deliberately conservative, the deep verifier needs nightly compiler internals, and the project is openly pre-1.0. But the core idea — a contract is a checked declaration, not a comment — feels especially useful when reviewing agent-generated code. Trusting that generated code followed a prompt is a lot weaker than making the compiler reject a lie.

I’d like to hear your views:

Would you use an effect system delivered this way — as an additional compiler pass over otherwise ordinary Rust — or does the dependency on compiler internals make it a non-starter for you?

(Everything above is based on the shipped MIR traversal, macro expansion, role-proof implementation, and the focused deep-verification suite, which currently passes including the indirect purity violation case.)


r/learnrust • • Aug 09 '26

Ruby or Rust to learn Programming?

Thumbnail reddit.com
0 Upvotes

r/learnrust • • Sep 10 '26

How do you review .unwrap() calls in AI-generated code before merging?

0 Upvotes

Got bitten recently by a panic in production, config.get("timeout").unwrap() in some AI-assisted code, no handling for the key actually being missing. Compiled fine, passed review, worked in every test, then paniced the one time an env var didn't get set during a deploy.

What's tripping me up as someone still learning Rust properly: I get that unwrap() is a deliberate choice, not a compiler error, but I don't have a great instinct yet for when it's actually fine (a value you've already validated exists) versus when it's a landmine (anything coming from external input, config, network, user data). Is there a rule of thumb you use, or does it really just come down to "any unwrap on something not fully controlled by your own code needs a real Result/Option handling path instead"? Also curious if clippy or some other tool actually flags risky unwraps specifically, or if this is still mostly a manual review thing.


r/learnrust • • Apr 13 '20

Before I leave reddit I wanted to say the mods here are top notch

7 Upvotes

Great mods here. I have zero problems with any of them.

I had a fight with mods from another sub (there's an image if you guys are curious enough to look at my post history) and the mods acted poorly to someone else who brought an issue up. Rust mods are the complete opposite and I wanted to publically say good job


r/learnrust • • May 23 '25

Day 6 of learning rust

0 Upvotes

Sorry I didn't update what I did on day 5 but ya surely did something : completed chap 12 I think it's time to slow down a bit .

I would love to get some assignments from you guys Few beginner level assignments so that I could have good grasp on my foundations .

Today day six i didn't study a bit . Today I am building a auction system using redis and ws . Soon I shall migrate this from ts to rust. May the day come soon when I write my own http server on rust .

See you folks If you are pro please assign me few basic assignments so that I learn by building.


r/learnrust • • Aug 12 '22

How to have multiple mutable references to one object?

1 Upvotes

Let us say we have this C code:

struct A {int i;}  
struct B {A* a;}
struct C {A* a;}

void main()

{

A a;
B b; b.a = &a;
C c: c.a = &a;

}

In this scenario A is stack allocated, B and C point to the stack allocated memory where A lives.

I need to do exactly the same thing in rust but every time I try to create mutliple mutable references it complaints.

It's a little frustrating having to fight the language to accomplish something so basic.


r/learnrust • • Jul 15 '24

Learn Rust

1 Upvotes

Any suggestion to get cleared about borrowing and reference still confused...... Suggest best free sourceto learn rust as a beginner to expert.

Pls....


r/learnrust • • Apr 04 '23

The first line is printed outside of any block, so it refers to the initial value of x which is 4. The second line is printed within the nested block, so it refers to the new variable x that was created within that block, which has a value of 2. The third line is printed after the nested block is 5

Post image
0 Upvotes

r/learnrust • • Sep 26 '25

Why does Rust have no debugger?

Thumbnail
gallery
0 Upvotes

So, i am trying to learn Rust AI-Free but i got a problem while writing my first 'Hello, world!', as you can see in the images (1-2) there is no 'Rust debugger' extension, i tried installing CodeLLDB and it just gets me blank with 'Spawn cargo ENOENT' even when i activate the virtual enviroment, does anyone want's to help me out to the debugger?

(Note: I won't be replying unless is a debugger comment)


r/learnrust • • Apr 30 '26

Git Implementation in Rust (Atleast some parts of it)

2 Upvotes

https://github.com/prranavv/r-git . Would like any feedback on it


r/learnrust • • Aug 06 '25

A thousand roads and all of them muddy?

0 Upvotes

My programming knowledge is that obtained in the Harvard CS50 course. And I choose rust because the next learnings will be related to Blockchain, Cryptography, a little bit of cybersecurity, etc.

I know, it's a somewhat difficult language to learn. The thing is that I intend to optimize resources and simply not invest time learning badly or inefficiently.

They have given me several learning paths and resources, but surely you have better judgment and want to share. He stressed that at the same time he would be studying Flowcharts and it is a kind of good approach.


r/learnrust • • Jan 03 '23

Why are rustaceans bad teachers ?

0 Upvotes

I do not understand why it is done "like that". The official site (and book), the other books, the courses, the problems. All start very slowly, with variables, loops, conditions. Soooo slow, boring. It is necessary, but why THAT slow ?

Then, with 5 minutes of confuse explications, it jumps to something incomprehensible, with traits, (non)heritage, borrowing, even ,...

When I learned Python or C++, after the basics, there are large parts on classes, heritages, all the "complex" stuff. Here, 100 pages on the very easy, 5 pages (really !) on the hard stuff and lets go for alchimic formulas no "newbie" will decipher, even in 100 years. One book, another book, a course, another course. All the same shit of pages of "println!("hello rustaceans"); (with "I dont explain macros now as it is too complicated", we will see them in chapter XXIV p 431...).

Rust seems very good. But why are rustaceans so bad teachers ?