MCPcopy Create free account
hub / github.com/QMHTMY/RustBook / binary_search2

Function binary_search2

publication/code/chapter06/binary_search.rs:27–40  ·  view source on GitHub ↗
(nums: &[i32], num: i32)

Source from the content-addressed store, hash-verified

25}
26
27fn binary_search2(nums: &[i32], num: i32) -> bool {
28 // 基本情况1: 项不存在
29 if 0 == nums.len() { return false; }
30
31 let mid: usize = nums.len() >> 1;
32 if num == nums[mid] {
33 // 基本情况2: 项存在
34 return true;
35 } else if num < nums[mid] {
36 return binary_search2(&nums[..mid], num);
37 } else {
38 return binary_search2(&nums[mid+1..], num);
39 }
40}
41
42fn main() {
43 let nums = [1,3,8,10,15,32,44,48,50,55,60,62,64];

Callers 1

mainFunction · 0.70

Calls 1

lenMethod · 0.45

Tested by

no test coverage detected