Rust Programming Exercises: Binary Search

preview_player
Показать описание
In this video we will implement the classic binary search algorithm over a sorted collection of integers. We will implement several variations using the standard while loop with if/else conditions as well as one with the match control operator in Rust. Additionally, we'll take a look at the std::cmp::Ord trait and using generics to make our function more flexible.

↠ References:

Cheers! 🍻

🦀 #rust #rustlang #rustprogramming #exercises #leetcode #programming #algorithms
Рекомендации по теме
Комментарии
Автор

You forgot to catch the overflow bug in your midpoint calculation. `(l + r) / 2` can overflow because you're adding 2 variable integers. Instead you can use `l + (r - l) / 2` which is safu from overflow as long as you know that l < r, which you do in this case.

tvboyd
Автор

You deserve more subs pal ...such great content.

MrAnandml
Автор

Just a tip dont use else on an if statement with a return. other than that cool stuff did not know about cmp

darkenblade
Автор

can you share your setup details, which extension is it with neovim.

sangamo
Автор

Thank you so much for your videos. I am starting on rust and i am learnign a lot from your videos. I sincerely hope you can continue making more.

one small thing to call out, probably also needs double checking from you is that, when we define the input parameter as `AsRef`, we need to be cautious that the function still takes ownership from the input parameter. So in this case, using the function you have defined in the video, once a vector is used for binary search, it is no longer usable.

I think if that is right, then from the design perspective, it may be better to make the input parameter simply &[T]?

hairuilin
Автор

how did you structured your code so you can have the tests in a total separated directory?

RichardNatal
Автор

Thanks a lot for your explanation. I tried this code and found that the case when L equals R is not handled. I guess this case is possible.

paulineputrolaynen
Автор

Can you please tell if instead of "return Some(mid);" when i used "Some(mid)" it gave error. Why?

vishalgupta