How to implement T/2.0 T is a Generic


#1

Merry Christmas!

I now writing a binary search program using rust,When calculate the mid((start+end)/2) in c++/c ,I can write like (start+end)/2.0 ,but in rust it’s invalid.how can I implement it? Thank you for you answer!


#2

Something like https://play.rust-lang.org/?gist=df7a062a96c4fa91d89c83a91724eaec&version=stable

Should also mention that you may want to use an overflow avoiding construction: start + ((end - start) / 2)


#3

Thanks.