实现说明
partition_point 找分界点以函数 main为骨架。
使用要点
- 直接运行文件,输出会带上标签,比如
first >= 20ms: index、slow tail:、empty boundary:——输出的每一行都能指回产生它的那次调用。
常见问题
运行「partition_point 找分界点」会打印什么?
文件本身就是完整程序,直接运行——输出行带有 first >= 20ms: index、slow tail:、empty boundary: 这类标签。
核心语句
let within_budget = latencies_ms.partition_point(|&ms| ms <= 30);
let first_slow = latencies_ms.partition_point(|&ms| ms < 20);
let latencies_ms = [3, 9, 12, 18, 25, 31, 44];
代码示例
fn main() {
let latencies_ms = [3, 9, 12, 18, 25, 31, 44];
// partition_point:有序序列中第一个让谓词返回 false 的下标
// partition_point finds the boundary in sorted data
let first_slow = latencies_ms.partition_point(|&ms| ms < 20);
println!(
"first >= 20ms: index {} value {}",
first_slow, latencies_ms[first_slow]
);
println!("slow tail: {:?}", &latencies_ms[first_slow..]);
// 谓词必须单调:前面全 true,后面全 false
let within_budget = latencies_ms.partition_point(|&ms| ms <= 30);
println!("{} samples within 30ms", within_budget);
let empty: [u32; 0] = [];
println!("empty boundary: {}", empty.partition_point(|_| true));
}