Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why doesn't this Rust program crash?

Consider this Rust program:

fn main() {
    let mut z : Vec<Vec<(bool,f64)>> = Vec::with_capacity(10);
    unsafe { z.set_len(10); }
    z[0] = vec!((true,1.));
    println!("{:?}", z[0]);
}

https://play.rust-lang.org/?gist=ccf387ed66a0d8b832ed&version=stable

Rust should attempt to drop z[0] when we set it, and since z[0] is uninitialized it should crash the program. However, it runs fine. Why?

like image 489
yong Avatar asked Jul 09 '15 03:07

yong


People also ask

How to fix rust crashing on startup?

If your Rust keeps crashing on startup, you should try running Steam as administrator to see if it works. Step 1: Completely exit Steam. Then right-click the Steam shortcut on your desktop and select Properties.

Why does rust keep crashing while loading?

When a certain game file is corrupted or missing, you may encounter the problem that Rust keeps crashing while loading. In this case, you can verify the integrity of your game files in Steam. If Steam detects any problematic files, it will fix them automatically.

Why is rust not launching on Windows 10?

Crash At Launch, Game Not Launching Fix One of the reasons behind Rust isn’t launching for you could be your anti-virus software or Windows Defender. You can fix this problem by either disabling the anti-virus or excluding the game’s folder from your anti-virus.

How do I run Rust as an administrator?

All your games are listed in Steam Library, find RUST and right-click it then select “Manage” and click “Browse Local Files”. In the pop up window, select “RustCLient” application and right-click it and click “Properties”. Click on “Compatibility Tab” and checkmark the checkbox beside “Run this program as an Administrator”. Click “Apply” and “OK”.


1 Answers

While the memory in the Vec’s heap allocation is uninitialised, it will most commonly be filled with zeros, and a zeroed Vec is an empty Vec (String and Vec have cheap constructors because they don’t make an allocation for an empty array). There is thus no allocation to free, and so it doesn’t crash in this particular case. Very slight modifications, or running it on a machine with slightly different uninitialised memory semantics, could easily cause it to crash (which would be a good thing—crashes are typically easier to debug than subtle errors).

This diagnosis can be seen to be the case.

like image 70
Chris Morgan Avatar answered Nov 15 '22 09:11

Chris Morgan