News for hackers. Stuff that matters.
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