Logo

Programming-Idioms

History of Idiom 124 > diff from v23 to v24

Edit summary for version 24 by programming-idioms.org:
[Rust] Comments emphasis

Version 23

2018-06-12, 17:36:13

Version 24

2018-08-03, 20:48:06

Idiom #124 Binary search for a value in sorted array

Write function binarySearch which returns the index of an element having value x in sorted array a, or -1 if no such element.

Idiom #124 Binary search for a value in sorted array

Write function binarySearch which returns the index of an element having value x in sorted array a, or -1 if no such element.

Extra Keywords
dichotomic dichotomy
Extra Keywords
dichotomic dichotomy
Code
a.binary_search(&x).unwrap_or(-1);
Code
a.binary_search(&x).unwrap_or(-1);
Comments bubble
This only works if a is an array of signed integers.
Generally, a Result or Option would be more useful.
Comments bubble
This only works if a is an array of signed integers.
Generally, a Result or Option would be more useful.
Doc URL
https://doc.rust-lang.org/nightly/std/primitive.slice.html#method.binary_search
Doc URL
https://doc.rust-lang.org/nightly/std/primitive.slice.html#method.binary_search