Rust's Two Kinds of 'Assert' Make for Better Code
1–10 of 87 posts
Re: Rust's Two Kinds of 'Assert' Make for Better Code
#2It’s trivial to write a debug_assert macro in C, so no, you’re not forced to do that.
Re: Rust's Two Kinds of 'Assert' Make for Better Code
#3> The problem that I’ve just expressed ultimately occurs because languages like C force us to encode both kinds of assumption with a single assert statement: either all asserts are compiled in or none are. It’s trivial to write a debug_assert macro in C, so no, you’re not forced to do that.
Re: Rust's Two Kinds of 'Assert' Make for Better Code
#4Re: Rust's Two Kinds of 'Assert' Make for Better Code
#5Re: Rust's Two Kinds of 'Assert' Make for Better Code
#6In the example of "min(ages) > 0", making age a NonZero type renders the assert unnecessary. Rust even has some fancy perf optimizations it can do with that information. It's a win all around.
Re: Rust's Two Kinds of 'Assert' Make for Better Code
#7Re: Rust's Two Kinds of 'Assert' Make for Better Code
#8Asserts are convenient, but every time I encounter them I ask "Can the compiler prove this can't happen?" In the example of "min(ages) > 0", making age a NonZero type renders the assert unnecessary. Rust even has some fancy perf optimizations it can do with that information. It's a win all around.
How would one communicate properties/traits of a custom type (like a non-zero unit) that a compiler can leverage to optimize (in general, for any programming language)?
Re: Rust's Two Kinds of 'Assert' Make for Better Code
#9 (assert (
That will print a message when the index is too large and give you the option of providing another index.Re: Rust's Two Kinds of 'Assert' Make for Better Code
#10Asserts are convenient, but every time I encounter them I ask "Can the compiler prove this can't happen?" In the example of "min(ages) > 0", making age a NonZero type renders the assert unnecessary. Rust even has some fancy perf optimizations it can do with that information. It's a win all around.
If you created a custom NonZero type, how would the Rust compiler figure out how to optimize that? How would one communicate properties/traits of a custom type (like a non-zero unit) that a compiler can leverage to optimize ( in general, for any programming language )?
An example of the niche optimisations is in that link; if a 32bit number is NonZero, you can put that in an Option, and your Option> will be the same size as a normal u32.