Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

Can this be used somehow for the bounded range? some like,

    struct MyIndex<const MIN: usize, const MAX: usize> {
        index: usize,  MIN/MAX??
    }
That would get rid of lots of runtime bound checks.


Not as things stand now. For this functionality you would want something closer to `I32<0..500>` and `struct I32<const RANGE: Range>(i32)`, so then operations between two `I32<RANGE>` can check what each corresponding const param is. I don't know if this will be possible in the short term, but I know that quite a few people want something along these lines, so I think it might happen at some point.


Yes, that would be ideal. Was just thinking whether the const generics can be hacked to do the same thing.


You can't use field access today, so having a const param `struct Range<T> { start: T, end: T }` isn't yet useful, but you can still get something working, although the error message is terrible:

https://play.rust-lang.org/?version=nightly&mode=debug&editi...

Edit: I'm honestly surprised at how usable this is already in the current state of nightly:

https://play.rust-lang.org/?version=nightly&mode=debug&editi...


Excellent hack! This demonstrates it actually works. Just need to make it ergonomic for everyday work. That's awesome.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: