(nums: &[i32], num: i32)
| 1 | // sequential_search.rs |
| 2 | |
| 3 | fn 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 | |
| 20 | fn main() { |
| 21 | let num = 8; |