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

Function sequential_search

publication/code/chapter06/sequential_search.rs:3–18  ·  view source on GitHub ↗
(nums: &[i32], num: i32)

Source from the content-addressed store, hash-verified

1// sequential_search.rs
2
3fn sequential_search(nums: &[i32], num: i32) -> bool {
4 let mut pos = 0;
5 let mut found = false;
6
7 // found 表示是否找到
8 // pos 在索引范围内且未找到就继续循环
9 while pos < nums.len() && !found {
10 if num == nums[pos] {
11 found = true;
12 } else {
13 pos += 1;
14 }
15 }
16
17 found
18}
19
20fn main() {
21 let num = 8;

Callers 1

mainFunction · 0.70

Calls 1

lenMethod · 0.45

Tested by

no test coverage detected