diff --git a/codes/rust/chapter_sorting/quick_sort.rs b/codes/rust/chapter_sorting/quick_sort.rs index 77dcf1c9a8..cd44248e20 100644 --- a/codes/rust/chapter_sorting/quick_sort.rs +++ b/codes/rust/chapter_sorting/quick_sort.rs @@ -46,11 +46,13 @@ struct QuickSortMedian; impl QuickSortMedian { /* 选取三个候选元素的中位数 */ fn median_three(nums: &mut [i32], left: usize, mid: usize, right: usize) -> usize { - let l = nums[left], - m = nums[mid], - r = nums[right]; - if ((l <= m && m <= r) || (r <= m && m <= l)) return mid; // m 在 l 和 r 之间 - if ((m <= l && l <= r) || (r <= l && l <= m)) return left; // l 在 m 和 r 之间 + let (mut l, mut m, mut r) = (nums[left], nums[mid], nums[right]); + if ((l <= m && m <= r) || (r <= m && m <= l)) { + return mid; // m 在 l 和 r 之间 + } + if ((m <= l && l <= r) || (r <= l && l <= m)) { + return left; // l 在 m 和 r 之间 + } right }