News for hackers. Stuff that matters.
Re:Several core problems with Rust by lordnacho on Sunday November 23, 2025 @06:17PM (#46028357)
I tend to disagree.

- Compile speed. Why do people care so much? Use debug for correctness and iterating your code. You're hardly going to change much between runs, and you'll get an incremental compile. Let rust-analyzer tell you if there are errors before you even try compiling. Let your CI do release optimization in its own time, who cares if CI is slow?

- The cloudflare bug was not caused by rust. Every language needs some way to say "hey this thing is invalid", and that's what happened when the list of AI tools got too long (or whatever their business issue was). Making the app panic was a design choice, not a language choice. Every language has a way to not handle errors.

- Things having unsafe {...} does not make them unreliable. On the contrary, if you run into a memory issue in a rust program, you know where to look for it. The problem in c++ was that the whole program was unsafe, and you then had no way to narrow it down. Memory safety errors are like type errors: a large class of errors that are worth checking for. If you're writing good c++, you have a linter that checks similar things anyway, and if you're disciplined, you've also upgraded warnings to errors. FWIW, I do think the marketing over-emphasizes memory management, because there are a lot of these hard learned lessons that are default in rust but optional in c++.

- Mutable shared state: make bad designs hard to write. Mutable shared state is another one of these eternal bug sources. Use channels and pass messages.

Honestly, it reads like a lot of critiques of Rust: people haven't spent enough time with it, and are not over the learning curve yet. Of course everything is cumbersome before you're used to it.

Re:Several core problems with Rust by josephg on Sunday November 23, 2025 @06:31PM (#46028462)
> The cloudflare bug was not caused by rust

Yeah. Rust’s Option::None as like null in C++. Unwrapping an option is like checking if something is null and crashing. This "crash from an unwrap" is just a null pointer exception / segfault in any other language. The same bug would be trivial to write in any other language, and with more or less the exact same result. Its just - weirdly news because it was rust code. What?

> Honestly, it reads like a lot of critiques of Rust: people haven't spent enough time with it, and are not over the learning curve yet.

Exactly. I’ve spent years with rust and my complaints are very different. Well, I still wish it compiled faster but all the other stuff is noobie problems. As someone with more experience, my frustration points are things like how async blocks do compiler magic you can’t write yourself. I hate unnameable types. I think Pin is confusing and weird. And the syntax for interacting with raw pointers is unnecessarily inconvenient and ugly.

Yes, rust makes programs with a lot of shared mutable state awkward to write. So don’t program like that. In general, if you write code in language X as if it were a bad version of language Y, you're going to have a bad time. The right question here is if the "rust way" of thinking about your code results in a beautiful program. You can't answer that if you go in with too many preconceptions of how programs should be designed.

Re:Several core problems with Rust by tialaramex on Sunday November 23, 2025 @09:09PM (#46029598)
The Cloudflare bug was unwrapping a Result::Err not an Option::None.

Both Option and Result can be unwrapped and - to some extent not coincidentally - both can also be subject to the Try operator (?) which is arguably more correct here than unwrap because this can fail and perhaps the caller will have some plan to recover.

My list of peeves would be very different from yours. I would like to prohibit move of the dodgier as casts (requiring that you write out the conversion call you actually meant explicitly and then handling any edge cases) for example.

What sort of "interacting with raw pointers" are you thinking of that's too inconvenient and ugly for your liking?

Re:Several core problems with Rust by josephg on Sunday November 23, 2025 @09:50PM (#46029838)
> Both Option and Result can be unwrapped and - to some extent not coincidentally - both can also be subject to the Try operator (?) which is arguably more correct here than unwrap because this can fail and perhaps the caller will have some plan to recover.

I guess its equivalent to Go code like this:

    value, err := stuff()
    if err != nil {
        panic(err)
    }
Or in C:

    int result = stuff();
    assert(result >= 0);
If someone wrote code like that and the program crashed, nobody would attack Go or C. In all these cases (including rust), the programmer is expressing clear, explicit intent for the program to panic if the call failed. I've seen some people say .unwrap() should be named .unwrap_or_panic() or something to make that more clear? I dunno. It seems like a nothingburger to me. A buggy program crashed. They'll do that.

> What sort of "interacting with raw pointers" are you thinking of that's too inconvenient and ugly for your liking?

I have 2 complaints. First, I wish Rust had a -> operator or something like it. This sort of thing is horrible:

    (*(*(*ptr).foo).bar)
Secondly, rust's aliasing rules make writing pointer heavy code very difficult. I like to run my unsafe code through MIRI to make sure I'm not doing anything bad, and its incredibly subtle.

Here's an unsafe function which manages the internal state of a skip list:

https://github.com/josephg/jumprope-rs/blob/3981256e4e741d8b...

This code is insanely fragile. There are multiple pointers to different elements floating around. The only way I got miri to calm down was to dereference the node pointer constantly. Eg:

    *cursor.num_bytes -= (*node).str.len_bytes();
    let next = (*node).first_next().node;
Basically everything else I tried caused obscure aliasing problems. I think if I rewrote this code today I'd probably use a Vec and safe rust instead. I can much more confident write correct C code than correct unsafe rust code. I wish it were the other way around.

As a general principle, unsafe rust should be as simple and easy to reason about as possible so we can spot security problems. But rust's syntax makes unsafe code very complex. I don't think this complexity is in any way necessary.

Re:Several core problems with Rust by tialaramex on Monday November 24, 2025 @06:26AM (#46032885)
I see what you mean for -> although personally I haven't missed it (I spent many years writing C) I don't write much pointer twiddling in Rust so I'm the wrong person to have an opinion.

However surely the aliasing is actually a problem and so MIRI is annoyed because what you wrote might be wrong? If we hang on to (*node).first_next() but somehow node changes, we're no longer talking about the same thing, it's exactly the aliasing problem.

I'd have to (which I have not) examine in detail how your code works to offer an opinion beyond speculation, but I think my instinct is sympathy for the "Don't write aliases" approach.

Re:Several core problems with Rust by josephg on Monday November 24, 2025 @03:37PM (#46038985)
> I think my instinct is sympathy for the "Don't write aliases" approach.

Yes, requiring that nothing alias it’s much more conceptually elegant. But as I said, in practice it makes programming extremely treacherous. There are no warnings when you alias. No errors. 99% of the time you’ll get a working program with no bugs every time you build. You’re just - quietly - at the whim of llvm. Maybe it won’t compile correctly on some other architecture. Maybe a minor compiler bump, or different flags, will make your program totally broken. The only way to discover there’s even a potential problem is by running the code through MIRI.

I get why it’s like this but I think it’s a mistake. I think unsafe rust should be at least as expressive and pleasant to work with as C. Having quiet foot guns here seems antithetical to rust’s philosophy of safe ergonomic programming.

Re:Several core problems with Rust by Klonoar on Monday November 24, 2025 @03:11AM (#46031560)
Err, you can't use `?` with `Option` unless I've really missed a big feature somewhere. You'd have to map it to a `Result` then unwrap, but at that point you still need to define an error type.
Re:Several core problems with Rust by tialaramex on Monday November 24, 2025 @03:19AM (#46031610)
https://doc.rust-lang.org/std/option/enum.Option.html#impl-T...

I guess you've "really missed" this feature or more likely you are considering only the case where our function signature says we return Result and so early returning Option won't compile.

Because the modern "try v2" operator is a trait, it is implemented for Option, Result, and ControlFlow plus Poll variations.

Historically its ancestor the try! macro was provided only for Result, but that's a very long time ago now.

Re:Several core problems with Rust by Klonoar on Monday November 24, 2025 @07:40PM (#46041100)
I guess? If it's still languishing in nightly I'm not sure I consider it usable; I'm aware some people are fine to run nightly but I keep to stable releases.
Re:Several core problems with Rust by tialaramex on Monday November 24, 2025 @08:05PM (#46041257)
So, all of Try is unstable. This means your stable Rust isn't allowed to implement Try for your own custom types, however the syntax is stable, so since Try is implemented for all of Option, Result and ControlFlow, those all work with the ? syntax even though that implementation is marked unstable.

Let me give you another example you've probably used without realising it, when you write password.contains("foo") in Rust. Why can you write "foo" there? Rust doesn't have overloading, and yet you can pass strings, characters, and functions including lambdas - how can this work? They all implement core::str::pattern::Pattern, an unstable trait used to deliver this functionality for all string matching.

In fact mechanically what's happening here for Try is that ? always worked for Option and Result, the "try v2" feature isn't about the syntax at all, it's about the behind the scenes implementation of that syntax. But that's not important, the syntax is promised and the final implementation will almost certainly be tryv2 or something very similar.

Re:Several core problems with Rust by Klonoar on Monday November 24, 2025 @10:50PM (#46042181)
TIL. Thanks for noting!
Re:Several core problems with Rust by IshKebab on Monday November 24, 2025 @03:30AM (#46031670)
You have missed a big feature! They added support for that like a year ago or something.
Re:Several core problems with Rust by tialaramex on Monday November 24, 2025 @04:01AM (#46031852)
Years ago. In April 2021 this starts to appear as part of Try v2 implementation work.
Re:Several core problems with Rust by Klonoar on Monday November 24, 2025 @07:40PM (#46041096)
I guess? If it's still languishing in nightly I'm not sure I consider it usable; I'm aware some people are fine to run nightly but I keep to stable releases.
Re:Several core problems with Rust by IshKebab on Tuesday November 25, 2025 @02:44AM (#46043320)
No it's stable.
Re:Several core problems with Rust by adastra22 on Monday November 24, 2025 @04:08AM (#46031907)
Rust makes it very, very easy to .unwrap() something, making panic-crashes the default error handling behavior for a lot of rust programmers. But it does this without, e.g., Erlang's reliability services that will auto-relaunch code that crashes. The end result is NOT a very good user experience.

I say this as a Rust advocate and daily Rust programmer, btw.

Re:Several core problems with Rust by dontlaugh on Monday November 24, 2025 @05:59AM (#46032662)
It should probably have a different name.

But nil panics in Go are even easier, the compiler doesn’t even require you to opt in. I deal with that every day.

Re:Several core problems with Rust by estebank on Monday November 24, 2025 @11:43AM (#46036023)
When it comes to distributed services, unwrap is likely the behavior you do want, because the service is being run by a scheduler that detects failures, and a panic would be tied to a metric and page an sre. My read of the situation is that they were overwhelmed by their dashboards which made it harder to identify the root cause, but that is a common situation for these kind of events. I'm pretty sure that this exact sequence of events will never happen again to them because they will adjust their observability to be clearer, should this ever happen again.

For libraries and cli tools, you don't want panics except for things that are broken beyond any recovery. A cli tool should not panic directly, instead emitting human readable errors.

Re:Several core problems with Rust by josephg on Wednesday November 26, 2025 @05:23AM (#46056015)
Yeah. Its also weird that people online are fixating on unwrap - as if changing or fixing unwrap's semantics would have helped cloudflare here.

The best way to think about unwrap is that its like an assert. If an assert trips, the answer isn't to remove the assert and just hope for the best. Asserts are almost never the problem. The problem is whatever happened right before the assert. Ie, the buggy codepath that generated the erroneous state in the first place.

In cloudflare's case, the bug was that their code required that a database query returned less than 200 results. Only the database returned more results than that. This isn't a problem with unwrap. It was these two mutually incompatible pieces of behaviour colliding in code. I don't see why rust has anything to do with this at all. Sloppy programmers can make a mess in any language. Rust is no exception.

Re:Several core problems with Rust by PaulDavisThe1st on Sunday November 23, 2025 @08:38PM (#46029371)
> Compile speed. Why do people care so much?

Ardour is a real-world, mid-size project. About 1.3M lines of code, almost all of it C++. On the fastest typical x86_64 systems, it builds in about 1.5mins, on an M3 it builds in about 3.5mins, on a somewhat older x86_64 16 core system it builds in about 7.5mins.

If you ever touch libs/ardour/ardour/session.h or libs/pbd/pbd/stateful.h, you're in for an almost complete recompile. If you touch libs/temporal/temporal/timeline.h, same thing. That's just the day to day work of being a developer of a native desktop application - nothing to do with CI or releases.

That's why some of us care.

Re:Several core problems with Rust by jandrewrogers on Sunday November 23, 2025 @10:24PM (#46030031)
> Mutable shared state: make bad designs hard to write.

Also makes good designs hard to write, designs that can be significantly more efficient.

Passing messages is not a universal solution to shared mutable state. It is great for certain patterns but suboptimal for others.

Re:Several core problems with Rust by eddd-ddde on Monday November 24, 2025 @12:19AM (#46030622)
Wrong, it makes good designs easy to write with clear safe APIs. If you know what you are doing just create a safe wrapper over unsafe functions and do what you need.

For example you can just write a lockless triple buffer for efficient memory sharing and wrap the unsafe usage of pointers with a safe API. now you only need to pay attention to those specific unsafe calls.

Re:Several core problems with Rust by int3trap on Monday November 24, 2025 @09:43PM (#46041835)
> Wrong, it makes good designs easy to write

It encourages good designs but it does not make them easy to write, but that's somewhat the point. Its not trivial to design a safe API that pushes performance limits.

> just write a lockless triple buffer for efficient memory sharing and wrap the unsafe usage of pointers with a safe API

This isn't practical or pragmatic.

And I say this as someone who likes rust and develops in it every day.

Re:Several core problems with Rust by byko3y on Sunday November 23, 2025 @10:33PM (#46030082)
>Compile speed. Why do people care so much? Use debug for correctness and iterating your code. You're hardly going to change much between runs, and you'll get an incremental compile.

What's the largest Rust-based public codebase so far? Rustc with like 1 million lines of code including all the dependencies in all the languages? Zed IDE has 800k lines. Incremental compilation seems to work fine at this scale, but things become messy above it. Most people prefer to not exceed the limit.

>Mutable shared state: make bad designs hard to write. Mutable shared state is another one of these eternal bug sources. Use channels and pass messages.

People think BEAM (Erlang/Elixir) does not have shared mutable state because the programming model says so, but it actually has some mutable shared state transparently under the hood i.e. implementation details and runtime. BEAM processes all run in a single OS address space, they are not even separate threads but async tasks spread out across OS threads — and still per programming model of the language those are "shared nothing". So in the end BEAM is a very good abstraction on the partially shared mutable state.

And no, in BEAM remote messaging is not nearly the same as local messaging. For example, you don't have built-in supervision trees on remote, and ETS is also local-node-only.

I do agree that hardware model of shared mutable state is inherently problematic, but we don't have other commodity CPU-s yet — you have to handle the shared mutable if you wanna be close to hardware.

>It reads like a lot of critiques of Rust: people haven't spent enough time with it, and are not over the learning curve yet. Of course everything is cumbersome before you're used to it.

Well, at least you confirm that the complexity is real. Don't get my criticism to close to the heart though — I did exaggerated the problems a bit, because someone needs to counteract the fanboys.

Re:Several core problems with Rust by EnPissant on Sunday November 23, 2025 @11:47PM (#46030483)
More LLM spam from you.

Also you admit this is rage bait.

Re:Several core problems with Rust by Capricorn2481 on Sunday November 23, 2025 @10:56PM (#46030214)
> Things having unsafe {...} does not make them unreliable. On the contrary, if you run into a memory issue in a rust program, you know where to look for it

This isn't true, no matter how much people keep saying it. Unsafe does not scope bugs to the block, or even where you have to look for bugs. Just having unsafe in your codebase means changing code outside the unsafe block could cause UB.

Doesn't mean I think it's worth complaining about, there's really no better way for Rust to do this without sandboxing memory.

Re:Several core problems with Rust by vacuity on Sunday November 23, 2025 @11:32PM (#46030407)
Internal use of unsafe requires securing the safe code at the module boundary. However, the design of unsafe still greatly reduces the burden of memory safety, both when it is and isn't used directly. The specific semantics of Rust aside, unsafe is more or less the ideal way for a language to express unsafe escape hatch constructs.
Re:Several core problems with Rust by Capricorn2481 on Monday November 24, 2025 @02:51PM (#46038421)
> Internal use of unsafe requires securing the safe code at the module boundary

Not even that, because you can pass data structures from unsafe code at arbitrary depth.

Re:Several core problems with Rust by vacuity on Monday November 24, 2025 @05:45PM (#46040311)
If the goal is to make a safe abstraction, which is what I'm talking about, proper use of visibility and safe code at the module boundary encapsulates the unsafe code soundly.

You seem to be talking either about improper encapsulation or unsafe that is intended to be exported and used unsafely. Or perhaps some method that is not sound according to the language.

Re:Several core problems with Rust by Dylan16807 on Monday November 24, 2025 @01:11AM (#46030865)
If changing the safe code causes UB, the actual bug is in the unsafe code.
Re:Several core problems with Rust by tialaramex on Monday November 24, 2025 @06:49AM (#46033074)
This is very true from outside an encapsulation, but within the private implementation of a type there may be safe Rust that's required to uphold some assumptions made by the unsafe Rust [if there is some] and while in theory the bug is in the incorrect assumption made by the unsafe Rust in practice we're going to fix the "safe" code which broke our assumption instead.
Re:Several core problems with Rust by Capricorn2481 on Monday November 24, 2025 @02:55PM (#46038452)
Nope. I would encourage you to actually read what unsafe does, because nowhere in the Rust docs does it say "scopes bugs to the unsafe block"

See the below code. The unsafe code is doing exactly what it's supposed to. The safe code frees a value while it's being used. This compiles. There's nothing to change here in the unsafe code.

```rust

  use std::slice;
  
  // Unsafe block that creates a slice from raw parts
  fn make_slice(ptr: *const i32, len: usize) -> &'static [i32] {
      unsafe {
          slice::from_raw_parts(ptr, len)
      }
  }
  
  fn main() {
      let vec = vec![1, 2, 3, 4, 5];
      let ptr = vec.as_ptr();
      let len = vec.len();
      
      // Unsafe block creates the slice - this operation itself is fine
      let slice_ref = make_slice(ptr, len);
      
      // Safe code: drop the vec
      drop(vec);
      
      // Safe code: use the slice - CRASH/UB
      println!("{:?}", slice_ref);
  }
```
Re:Several core problems with Rust by aw1621107 on Monday November 24, 2025 @03:38PM (#46039001)
I think one can argue make_slice is in fact at fault here since it's unsound (i.e., it doesn't enforce the requirements of from_raw_parts() and so allows safe code to invoke UB) and therefore either should be marked unsafe or should actually enforce the requirements of from_raw_parts().

That being said, I think in this context that's more of a nitpick since you're right in stating that bugs that result in UB need not be scoped strictly to unsafe blocks.

Re:Several core problems with Rust by Dylan16807 on Monday November 24, 2025 @05:07PM (#46039971)
The unsafe code is not fulfilling the contract of "unsafe", which requires the coder to uphold the memory safety rules as seen from outside. make_slice is putting a very wrong lifetime on the slice it returns.
Re:Several core problems with Rust by Capricorn2481 on Tuesday November 25, 2025 @07:24AM (#46045152)
> The unsafe code is not fulfilling the contract of "unsafe", which requires the coder to uphold the memory safety rules as seen from outside

It's the EXACT opposite. From the Rust docs. [1]

> By calling an unsafe function within an unsafe block, we’re saying that we’ve read this function’s documentation and we take responsibility for upholding the function’s contracts

The caller here is what is violating the contract. It's not the unsafe code's job to do that. How could it? It's just making a slice, it doesn't know what from. It can't guarantee a slice it makes is ANY kind of lifetime. If it could, it wouldn't require unsafe. Implicitly, this function requires the structure it's making a slice from to be static.

The key here is I'm showing you the bad caller can be at arbitrary depth. The implementation of make_slice is not big enough to violate anything. It's how it's used that it becomes a problem.

I wrote a bad lifetime to illustrate that Rust doesn't care. Normally, the compiler would stop you from using a function with this lifetime like this, but not with unsafe. Because it's the calling code that has to care. And changing the lifetime here won't fix squat.

  // still broken
  // An unconstrained lifetime is as bad as a static lifetime. 
  fn make_slice<'a>(ptr: *const i32, len: usize) -> &'a [i32] {
Yes, if you change the function signature and require it to receive the vec so you can see the lifetime, you could technically fix it from this function. That's the only way to get the right lifetime.

  fn make_slice<'a>(ptr: *const i32, len: usize, _lifetime: &'a Vec<i32>) -> &'a [i32] {
So yes, as long as you change your function so much you can basically remove unsafe, you can fix it from the function.

But you can't do this in scenarios where you'd actually want unsafe, like FFI, custom data structures, etc.

[1] https://doc.rust-lang.org/book/ch20-01-unsafe-rust.html#call...

Re:Several core problems with Rust by lunar_mycroft on Tuesday November 25, 2025 @10:29AM (#46046704)
> How would you fix this by only editing the code inside the unsafe block?

In this case, the solution is to mark make_slice as unsafe. This should make sense if you think about it for a second, because it's a thin wrapper around an unsafe function. If it were possible to make a function which did what std::slice::from_raw_parts does safe, it wouldn't be marked as unsafe.

> It's the exact opposite. From the Rust docs.

The docs you quote say the exact opposite of what you claim. Where do you do call the unsafe function in an unsafe block? In make_slice, not main.

> So yes, as long as you change your function so much you can basically remove unsafe, you can fix it from the function.

The actual use case for std::slice::from_raw_parts is for when you control the pointer and length and thus can guarantee that they're valid (specifically, by encapsulating them and making sure no safe functions can get them into an invalid state). For example, std::Vec::as_slice uses it [1] to get a slice from the pointer to it's managed buffer and it's stored length.

> FFI

There are two possible ways FFI could cause memory safety violations despite being called from safe code:

- The safe wrapper makes incorrect assumptions about the contract of the functions it's calling (e.g. maybe it assumes that a c function returns a pointer which it's the caller's responsibility to free, but is actually freed by the c code). In this case, the bug is in the unsafe rust. - The foreign code violates it's contract (e.g. same as above, but the documentation of the c code claims otherwise). In that case, the bug is in the foreign code.

Either way, the memory safety bug is not in safe rust.

[1] https://github.com/rust-lang/rust/blob/7934bbdf84a6b9a30297c...

Re:Several core problems with Rust by Capricorn2481 on Tuesday November 25, 2025 @05:08PM (#46051397)
> The actual use case for std::slice::from_raw_parts is for when you control the pointer and length and thus can guarantee that they're valid (specifically, by encapsulating them and making sure no safe functions can get them into an invalid state)

Right...so you fix it from the safe code, because the safe code is in charge of using from_raw_parts correctly.

> Either way, the memory safety bug is not in safe rust

This might just be a difference of semantics between us, but I really don't see how you can arrive at that conclusion. Just remove the function entirely and let's use from_raw_parts directly. The bug is literally not in unsafe, it's when we drop the vec after making a slice from it. To fix this, we would not change anything in unsafe.

  let vec: Vec<i32> = vec![1, 2, 3, 4, 5];
  let ptr = vec.as_ptr();
  let len = vec.len();
  
  let slice_ref = unsafe {slice::from_raw_parts(ptr, len)};
  
  drop(vec);
  
  println!("{:?}", slice_ref);
Re:Several core problems with Rust by Dylan16807 on Tuesday November 25, 2025 @05:18PM (#46051496)
> Right...so you fix it from the safe code, because the safe code is in charge of using from_raw_parts correctly.

Safe code cannot corrupt the Vec variables to make Vec do something incorrect.

> To fix this, we would not change anything in unsafe.

You're supposed to.

Some coders might implicitly count the rest of the function as trusted code that's part of enforcing the invariants of the unsafe code. So there's an implied "unsafe" block that fills up the rest of the function, as far as responsibility goes.

I won't argue one way or the other on that philosophy, but if anything outside the function with the unsafe block is part of enforcing invariants, then that is clearly wrong.

If you subscribe to that philosophy, then you could say the drop is the problem in this single-function case. But in the original the bug is still inside make_slice, because make_slice is unleashing a fragile and uncontrolled slice into the safe world of main.

(There might be some more subtleties when it comes to class API boundaries but I don't want to dig through that right now, it's not relevant to the important part of this conversation.)

Re:Several core problems with Rust by Capricorn2481 on Tuesday November 25, 2025 @05:28PM (#46051588)
>> To fix this, we would not change anything in unsafe.

> You're supposed to.

To me, it just sounds like we're disagreeing on definitions. You can view it as unsafe corrupting the vec, I don't disagree with that, but it's the way the safe code coordinated the unsafe code that caused that problem.

I'm happy to be shown a different way. Would you kindly show me how you would fix this in the unsafe block only?

> Some coders might implicitly count the rest of the function as trusted code that's part of enforcing the invariants of the unsafe code. So there's an implied "unsafe" that fills up the rest of the function, as far as responsibility goes

That's essentially what I'm arguing.

> but if anything outside the function with the unsafe block is part of enforcing invariants, then that is clearly wrong

What is clearly wrong? I'm not saying none of it is wrong, I'm just saying unsafe allows you to make bugs outside of it. And yes, it can happen at arbitrary depth, not just in the function containing unsafe code.

Re:Several core problems with Rust by Dylan16807 on Tuesday November 25, 2025 @05:35PM (#46051664)
> Would you kindly show me how you would fix this in the unsafe block only?

I'd probably hold a reference to the vector or something.

> That's essentially what I'm arguing.

The thing is it's only the safe code in the same function as the unsafe block that you could call responsible in any reasonable sense.

In the original version the main() code cannot be blamed for dropping a vector 'early', because it didn't invoke this scheme that requires the vector to be kept alive, make_slice did. It was make_slice's responsibility to guarantee the underlying allocation stays alive, and it didn't do that.

> What is clearly wrong? I'm not saying none of it is wrong, I'm just saying unsafe allows you to make bugs outside of it. And yes, it can happen at arbitrary depth, not just in the function containing unsafe code.

If you're putting responsibility on code in the same function, you're not really treating it as safe code. There's an unsafe{} around the entire function body that you should have written if you were going for maximum clarity.

If the depth goes any higher, you screwed up. It should be impossible for safe code outside the function to use your code and cause a memory error. If it's not impossible then that's a bug in the unsafe blocks (or the function with the unsafe blocks).

Re:Several core problems with Rust by Capricorn2481 on Tuesday November 25, 2025 @05:49PM (#46051800)
> I'd probably hold a reference to the vector or something

In a way that only changes the unsafe block?

> make_slice's responsibility to guarantee the underlying allocation stays alive, and it didn't do that

We çan talk about the simpler code example. How can my unsafe block make sure an allocation stays alive past the unsafe block itself?

> If you're putting responsibility on code in the same function, you're not really treating it as safe code. There's an unsafe{} around the entire function body that you should have written if you were going for maximum clarity

I'm not talking about what should've been done, I'm saying this is something to be aware of on projects with multiple people who may not catch something like this. People think bugs cannot happen in safe code. I just showed it happening. The bug was not in unsafe, or in the drop, it was in println.

Re:Several core problems with Rust by Dylan16807 on Tuesday November 25, 2025 @06:07PM (#46051933)
> How can my unsafe block make sure an allocation stays alive past the unsafe block itself?

Put it in another data structure or something?

Listen, if you can't make the invariant work, then you need to change the function. An unfixable unsafe is not an excuse to allow errors

> I'm saying this is something to be aware of on projects with multiple people who may not catch something like this.

It's good to be aware but put the blame in the right place. It's the unsafe code that's actually at fault. If you are seeing corruption, look at the unsafe code first with an adversarial mindset.

> The bug was not in unsafe, or in the drop, it was in println.

N. O.

Any unsafe code that outsources invariant enforcement that affects memory safety to safe code has bugs. There's wiggle room on "outsources" but that's the only wiggle room.

Re:Several core problems with Rust by Capricorn2481 on Tuesday November 25, 2025 @09:43PM (#46053654)
> It's good to be aware but put the blame in the right place. It's the unsafe code that's actually at fault. If you are seeing corruption, look at the unsafe code first with an adversarial mindset.

Yeah, that's why my first comment in this thread was "Just having unsafe in your codebase means changing code outside the unsafe block could cause UB". You can caveat that with "that's bad code", but that's the reality.

> An unfixable unsafe is not an excuse to allow errors

Nobody is saying that, you're just moving goalposts now. Nobody is saying you should allow errors, I'm telling you that everyone, including you, will inevitably have to change the code around the unsafe block, because that's what has to enforce memory at the end of the day.

You said you don't need to look at the safe code, I'm asking how would you fix the unsafe, and you haven't. That's fine, and I'm not faulting the language for it.

>> The bug was not in unsafe, or in the drop, it was in println.

> N. O.

Of course it is. That line becomes instructions that access memory freed by the previous line, drop(vec). That's called a dangling pointer. You remove println and the slice is now dropped immediately. You remove drop and the println will work. The vector is not "corrupted" by unsafe. That's not how computers work. We just lose guarantees from Rust when using unsafe, including in safe code. Doesn't mean there's a bug. That's the whole point of unsafe, is to be trusted by the compiler.

> Any unsafe code that outsources invariant enforcement that affects memory safety to safe code has bugs.

All useful unsafe code outsources invariants to safe code. If we could verify the integrity of the memory, we wouldn't be using unsafe.

Now you have been interchangeably using unsafe to mean the literal blocks and the surrounding code. But here is my point: If you are saying that "unsafe code" means "just the unsafe blocks," then yes, unsafe fundamentally relies on safe code to do the right thing.

But if "unsafe code" means "everything that must uphold the invariant," then unsafe can span your entire codebase. Which is true. Just the presence of unsafe in your code base means you're looking at UB anywhere in the call stack if people don't pay attention. And that's been my whole point of this thread. The presence of unsafe means everyone now has to pay attention not just to the safe block, but all safe code interacting with that data, especially in multi-threaded scenarios.

Re:Several core problems with Rust by Dylan16807 on Wednesday November 26, 2025 @02:32AM (#46055045)
> Yeah, that's why my first comment in this thread was "Just having unsafe in your codebase means changing code outside the unsafe block could cause UB". You can caveat that with "that's bad code", but that's the reality.

I agreed with you that changing safe code could trigger the bug, but the safe code is not where the bug is.

> Nobody is saying that, you're just moving goalposts now. Nobody is saying you should allow errors, I'm telling you that everyone, including you, will inevitably have to change the code around the unsafe block, because that's what has to enforce memory at the end of the day.

When fixing a vulnerable unsafe block, you might have to redesign the unsafe API, and that might require changing some safe code.

Once you decide on an API, you will not have to change safe code. The unsafe code handles all of the enforcement. If safe code is enforcing anything, you broke the rules of unsafe.

> You said you don't need to look at the safe code, I'm asking how would you fix the unsafe, and you haven't. That's fine, and I'm not faulting the language for it.

I'm not an expert at rust. I gave a couple prose suggestions but they require redesigning the way the safe and unsafe code talk to each other. Because your original design is inherently flawed. The unsafe code cannot protect itself, so it must not be used this way. You're saying we should make the safe code protect the unsafe code, and that is not right. Unsafe code needs to protect itself.

> Of course it is. That line becomes instructions that access memory freed by the previous line, drop(vec). That's called a dangling pointer. You remove println and the slice is now dropped immediately. You remove drop and the println will work. The vector is not "corrupted" by unsafe. That's not how computers work. We just lose guarantees from Rust when using unsafe, including in safe code. Doesn't mean there's a bug. That's the whole point of unsafe, is to be trusted by the compiler.

"unsafe" means "trust me compiler, I verified this myself"

Losing the guarantee is a bug. You told the compiler it didn't need to prevent a dangling pointer via the unsafe block, that you would prevent a dangling pointer via the unsafe block, and then you didn't prevent it.

If you didn't tell the compiler to trust you, the part that wouldn't have compiled is the unsafe block. You tricked it into compiling that block, so that block is where the bug is.

> All useful unsafe code outsources invariants to safe code.

That's extremely untrue. Lots of data structures protect all their invariants in their unsafe code.

> If we could verify the integrity of the memory, we wouldn't be using unsafe.

"we" are smarter than the compiler. Unsafe is for things "we" can verify but the compiler cannot. You're not supposed to use it for unverified stuff.

> Now you have been interchangeably using unsafe to mean the literal blocks and the surrounding code. But here is my point: If you are saying that "unsafe code" means "just the unsafe blocks," then yes, unsafe fundamentally relies on safe code to do the right thing.

If you're doing things 100% properly, you will expand your unsafe blocks to include everything that verifies and upholds invariants. But even after that expansion, it's still going to be a tiny fraction of your codebase.

> But if "unsafe code" means "everything that must uphold the invariant," then unsafe can span your entire codebase.

Not if your design is competent.

> Just the presence of unsafe in your code base means you're looking at UB anywhere in the call stack if people don't pay attention. And that's been my whole point of this thread.

"if people don't pay attention" is a huge factor here. If your unsafe code is wrong then that makes it hard to write safe code. But if you go fix the unsafe code then you stop needing to worry about safe code triggering a memory error.

> The presence of unsafe means everyone now has to pay attention not just to the safe block, but all safe code interacting with that data, especially in multi-threaded scenarios.

If you did things correctly, any safe function can be ignored for memory safety. Unsafe blocks are supposed to assume that the safe code calling them is actively malicious, and make themselves impossible to misuse.

Re:Several core problems with Rust by vacuity on Wednesday November 26, 2025 @09:01AM (#46057479)
See my comment to @Capricorn2481 here[0]. You seem to be saying that unsafe code can be made correct so as to ensure soundness. This can be done by verifying invariants in unsafe code, but it is generally discouraged, as it tempts large unsafe blocks that could be partially safe code. Both you and @Capricorn2481 don't seem to make the distinction between "arbitrary safe code" and "my safe code within the module with unsafe", which is a crucial part of the idea of writing encapsulated unsafe. Technically, I believe unsafe code can still be made sound in the presence of arbitrary safe code, but it would have to tiptoe around violating memory safety, lowering its utility and performance substantially, if not completely.

[0] https://news.ycombinator.com/item?id=46057382

Re:Several core problems with Rust by Dylan16807 on Wednesday November 26, 2025 @02:43PM (#46061544)
I accept that criticism. It's just that it makes things more complicated and harder to argue about (and I couldn't remember the exact boundaries). I was making the distinction earlier but dropped it for the sake of explaining the point of safe/unsafe.

So the categories of code: the unsafe block, the friend code that shares responsibility for invariants, the outside world code

Capricorn2481's mistake is putting the entire program in the second category, when only code in the same module is supposed to be there.

If a memory violation happens, you know you have a bug in a module containing unsafe blocks, which helps a lot. Those modules should be relatively rare and as small as possible.

Re:Several core problems with Rust by vacuity on Wednesday November 26, 2025 @08:50AM (#46057382)
As I said here[0], although I can't speak for what @Dylan16807 intends, invariants required by unsafe code are required exactly to the extent that some code can alter the invariants (the module boundary). In this sense, Rust's unsafe is just a particular example of encapsulation, where all notions of invariants in programming have the same essence.

[0] https://news.ycombinator.com/item?id=46030407

Re:Several core problems with Rust by lunar_mycroft on Wednesday November 26, 2025 @12:04PM (#46059633)
> Right...so you fix it from the safe code, because the safe code is in charge of using from_raw_parts correctly.

No, you fix it from the unsafe code (just not your unsafe code, since the API you're trying to expose is not possible to make safe). You encapsulate the arguments to from_raw_parts, provide a safe interface, and only manipulate the encapsulated data inside other unsafe blocks.

> This might just be a difference of semantics between us, but I really don't see how you can arrive at that conclusion. Just remove the function entirely and let's use from_raw_parts directly. The bug is literally not in unsafe, it's when we drop the vec after making a slice from it.

I think you're correct that semantics are at the core of our disagreement. Specifically, there are three different possible ways to answer the question of where a bug is:

1. Where the bug manifests a problem (produces an incorrect value, segfaults, etc).

2. Where the bug is directly caused.

3. Where the incorrect code is.

In the code you provided, 1 happens at the println, 2 happens at the drop, but 3 happens in the unsafe block. All your safe code is correct according to rust's memory safety rules rules (which is why it would pass the borrow checker), the problem is that by using an unsafe block you promised to uphold those rules yourself, and have failed to do so.

Re:Several core problems with Rust by Dylan16807 on Tuesday November 25, 2025 @01:16PM (#46048809)
> The caller here is what is violating the contract.

"The caller" of the unsafe function is the unsafe block in make_slice. And yes it's violating the contract.

> How would you fix this by only editing the code inside the unsafe block? Because you said the problem is the lifetime. That's outside of the unsafe block.

If it's locked into returning a static lifetime, then the only fix is to return a slice into a static allocation.

Is this the functionality you wanted? Probably not. But an unsafe block having an awkward API isn't an excuse to violate safety. Sometimes the only answer is to say no. Calling from_raw_parts this way is a clear bug.

> How could it? It's just making a slice, it doesn't know what from.

It could know more if you added more to the unsafe block outside the call to from_raw_parts.

> It can't guarantee a slice it makes is ANY kind of lifetime. If it could, it wouldn't require unsafe.

> as long as you change your function so much you can basically remove unsafe, you can fix it from the function.

from_raw_parts exists for situations where the coder can guarantee that the lifetime is valid (no matter what safe code does), but the compiler can't guarantee it (because it's not smart enough).

It's not there for situations where you make the safe code promise to behave.

It does have a lot of uses, in places that do need unsafe. You just happened to make an example that doesn't need it.

Re:Several core problems with Rust by Capricorn2481 on Tuesday November 25, 2025 @05:01PM (#46051322)
It's not a bug until main uses it incorrectly.

The lifetime is outside of the unsafe block. I can change it from static and we still have the same bug.

Re:Several core problems with Rust by Dylan16807 on Tuesday November 25, 2025 @05:11PM (#46051417)
> It's not a bug until main uses it incorrectly.

It's always a bug. The lifetime is wrong.

The mere ability to "use it incorrectly" and break memory safety is evidence of a bug. Safe rust in a program without unsafe can't use anything incorrectly in that way. Safe rust in a program with appropriately careful unsafe can't use anything incorrectly in that way.

> The lifetime is outside of the unsafe block.

The unsafe block uses the types from the surrounding function, so you can't change those types without double checking the unsafe block is still valid.

> I can change it from static and we still have the same bug.

Change it to something correct and have the same bug?

Because your continued examples look like no. If you make the lifetime arbitrary then it's still messed up, and if you get the lifetime off the vec then the bug is fixed.

Re:Several core problems with Rust by Capricorn2481 on Tuesday November 25, 2025 @05:13PM (#46051443)
Forget lifetimes, do this. It's a bug. It's in the safe code. You fix it in the safe code, because it misused unsafe code.

  let vec: Vec<i32> = vec![1, 2, 3, 4, 5];
  let ptr = vec.as_ptr();
  let len = vec.len();
  
  let slice_ref = unsafe {slice::from_raw_parts(ptr, len)};
  
  drop(vec);
  
  println!("{:?}", slice_ref);
Re:Several core problems with Rust by Dylan16807 on Tuesday November 25, 2025 @05:23PM (#46051534)
Okay I ended up replying to this example over here: https://news.ycombinator.com/item?id=46051496
Re:Several core problems with Rust by imtringued on Monday November 24, 2025 @06:53AM (#46033093)
This is true. If you consider unsafe memory to be a separate region in Rust, your safe Rust code can still have pointers to unsafe memory. The only solution to this problem is to perform some sort of validation of the returned data structures every time they are returned by an unsafe block.
Re:Several core problems with Rust by vacuity on Sunday November 23, 2025 @11:28PM (#46030391)
Your point about compile times is very dismissive and wrong, but I agree aside from that. It's worth noting that the Rust team explicitly tracks, monitors and limits regressions in, and actively optimizes for compile times, and Rust community surveys always ask for better compile times. This is clearly a significant issue, and while Rust is never going to compile as fast as C or Pascal, it can and will improve so that it becomes more usable.
Re:Several core problems with Rust by a022311 on Monday November 24, 2025 @03:31PM (#46038902)
> - Compile speed. Why do people care so much? Use debug for correctness and iterating your code. You're hardly going to change much between runs, and you'll get an incremental compile. Let rust-analyzer tell you if there are errors before you even try compiling. Let your CI do release optimization in its own time, who cares if CI is slow?

Even if you know you're code compiles, it's often easier and faster to validate the logic or complex interactions by running the code. In large (and even not so large actually) projects, debug builds can be painfully slow, even on good hardware. It's important to have a tight feedback loop so that you can interate quickly.