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

Function binary_search2

code/chapter05/binary_search.rs:27–39  ·  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 return true; // 基本情况2: 项存在
34 } else if num < nums[mid] {
35 return binary_search2(&nums[..mid], num);
36 } else {
37 return binary_search2(&nums[mid+1..], num);
38 }
39}
40
41fn main() {
42 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