Get the latest tech news

Using Drop for safety in Rust


A deep dive into Rust’s vec::Drain and its Drop implementation as an example of how ownership prevents subtle bugs—memory and otherwise!

Here you can see that tail_start and tail_end represent anything after the section you are pulling out with drain, with a special bit of handling to guarantee memory safety when dealing with the original Vec ’s contents. Here, we can see that it is safe by inspection because we guarantee at construction that the tail_len is bounded by the length of the original vector — but we can (and Rust does) also do a bunch of extra dynamic analysis with the Miri tool to make sure that is true via extensive testing. Here, the point is to make sure we have a valid pointer provenance to use when dropping any values that are unused at the end of the original range specified when calling drain: Miri will (rightly!)

Get the Android app

Or read this on Hacker News

Read more on:

Photo of safety

safety

Photo of Drop

Drop

Photo of Rust

Rust

Related news:

News photo

UK internet watchdog gives social media companies three months to improve safety or face huge fines

News photo

OpenAI, GoogleDeepMind, and Meta Get Bad Grades on AI Safety

News photo

Show HN: Performant intracontinental public transport routing in Rust