# quick-rank **Repository Path**: eshangrao/quick-rank ## Basic Information - **Project Name**: quick-rank - **Description**: No description available - **Primary Language**: Unknown - **License**: MIT - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2026-08-28 - **Last Updated**: 2026-08-28 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # A library for calculating rankings of a collection of items / 一个用于任意结构体计算排名的库 # Usage / 用法 ```rust /// Computes rankings for a collection of items. /// /// # Arguments /// /// * `data` - A mutable slice of items to be ranked /// * `value_getter` - A closure that extracts the ranking value from an item /// * `update` - A closure that writes the computed rank back to an item /// /// # How It Works /// /// The function iterates over each element in `data`. /// For every item `T`, it invokes `value_getter(&T)` to retrieve the value `&V` used for ranking. /// A rank is then computed based on that value, and `update(&mut T, rank)` is called to store the result. /// /// 计算排名 /// /// # 参数说明 /// /// * data 要排名的数据清单 /// * value_getter 获取参与排名数据的方法 /// * update 保存排名的方法 /// /// # 内部调用过程 /// /// 该方法将遍历data清单, /// 针对每个T,调用value_getter(&T)获取要排名的数据值&V, /// 然后对该数据&V生成排名,并调用update(&mut T,排名)存储排名 /// pub fn rank(&self, data: &mut [T], value_getter: G, update: U) where G: Fn(&T) -> &V, U: Fn(&mut T, u32), V: PartialEq + PartialOrd ``` # Example / 示例 ```rust use quick_rank::Ranker; #[derive(Debug, Default)] struct TestData { value: u32, rank: u32, } impl TestData { fn new(value: u32) -> Self { Self { value, ..Default::default() } } fn value(&self) -> &u32 { &self.value } fn update_rank(&mut self, rank: u32) { self.rank = rank; } fn new_test_data() -> Vec { vec![ TestData::new(5), TestData::new(2), TestData::new(4), TestData::new(1), TestData::new(3), TestData::new(2), TestData::new(4), TestData::new(7), ] } } #[test] fn test_asc() { let mut data = TestData::new_test_data(); Ranker::Ascending.rank(&mut data, TestData::value, TestData::update_rank); let result: Vec<_> = data.iter().map(|item| (item.value, item.rank)).collect(); assert_eq!( result.as_slice(), [ (5, 7), (2, 2), (4, 5), (1, 1), (3, 4), (2, 2), (4, 5), (7, 8) ] ); } #[test] fn test_resv() { let mut data = TestData::new_test_data(); Ranker::Descending.rank(&mut data, TestData::value, TestData::update_rank); let result: Vec<_> = data.iter().map(|item| (item.value, item.rank)).collect(); assert_eq!( result.as_slice(), [ (5, 2), (2, 6), (4, 3), (1, 8), (3, 5), (2, 6), (4, 3), (7, 1), ] ); } ```