Live data from Hacker News

Doing First Grade Math in Rust's Type System

fprasx.github.io

1–10 of 43 posts

Re: Doing First Grade Math in Rust's Type System

#4
post #3

Can someone give an example of a useful application of this technique? Honest question, if it is just for fun that is fine with me.

Life critical mathematics: you could verify calculations in the type system which are too risky to allow to fail at runtime.

Re: Doing First Grade Math in Rust's Type System

#5
post #3

Can someone give an example of a useful application of this technique? Honest question, if it is just for fun that is fine with me.

Before const generics, you needed this kind of "types" to use integer values in generics, for example for fixed-length vectors/matrices/... (and const generics still have limitations).

https://doc.rust-lang.org/reference/items/generics.html#cons...

They came with 1.51: https://blog.rust-lang.org/2021/03/25/Rust-1.51.0.html

Re: Doing First Grade Math in Rust's Type System

#6
post #2

"is much faster and the one you should probably use in practice" Where is this useful in practice? Exercises maybe. Like doing planks for example. I have never needed to plank "in practice" other than to exercise.

As I've already said, nowadays there are const generics which do (almost) the same. But before them, these were the only way to use integer values in generics.

They came with 1.51: https://blog.rust-lang.org/2021/03/25/Rust-1.51.0.html

Re: Doing First Grade Math in Rust's Type System

#7
post #3

Can someone give an example of a useful application of this technique? Honest question, if it is just for fun that is fine with me.

While not nearly as involved, I've seen similar-ish techniques used for FFTs and similar algorithms, in order to precompute constants.

Here[1] is an example in C++ but given the article it seems you could do something similar in Rust.

[1]: https://github.com/tmolteno/template-fft/blob/master/fft_rea...

Re: Doing First Grade Math in Rust's Type System

#8
post #2

"is much faster and the one you should probably use in practice" Where is this useful in practice? Exercises maybe. Like doing planks for example. I have never needed to plank "in practice" other than to exercise.

Like Object Oriented Programming, Type State Programming is a design paradigm. Using them in appropriate scenarios are very powerful. I am a wannabe Rustacean.

Re: Doing First Grade Math in Rust's Type System

#9
post #2

"is much faster and the one you should probably use in practice" Where is this useful in practice? Exercises maybe. Like doing planks for example. I have never needed to plank "in practice" other than to exercise.

> Like doing planks for example. I have never needed to plank "in practice" other than to exercise.

You need it every day to, you know, hold that meat on your carcass.

Re: Doing First Grade Math in Rust's Type System

#10
post #3

Can someone give an example of a useful application of this technique? Honest question, if it is just for fun that is fine with me.

While not nearly as involved, I've seen similar-ish techniques used for FFTs and similar algorithms, in order to precompute constants. Here[1] is an example in C++ but given the article it seems you could do something similar in Rust. [1]: https://github.com/tmolteno/template-fft/blob/master/fft_rea...

You do that (as nowadays in C++) with compile time "constants" https://doc.rust-lang.org/reference/const_eval.html
Post reply on HN