CARVIEW |
Select Language
HTTP/2 200
date: Sun, 12 Oct 2025 07:26:51 GMT
content-type: text/html; charset=UTF-8
server: cloudflare
x-frame-options: DENY
x-content-type-options: nosniff
x-xss-protection: 1;mode=block
vary: accept-encoding
cf-cache-status: DYNAMIC
content-encoding: gzip
set-cookie: _csrf-frontend=cb4c8db469eeaa7cc954c3daccab8439e9f6a2208a3c4d81279dfdbb50a7ea02a%3A2%3A%7Bi%3A0%3Bs%3A14%3A%22_csrf-frontend%22%3Bi%3A1%3Bs%3A32%3A%22wZjk3L5FrVpVIt9SQ7YtNYGpzvuDXrEE%22%3B%7D; HttpOnly; Path=/
cf-ray: 98d4df10acddc1b8-BLR
Rust grouping/counting stuff - Pastebin.com
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Just some code the help learn Rust, traits, lifetimes, etc.
- mod group {
- use core::slice::Iter;
- use std::collections::HashMap;
- use std::hash::Hash;
- pub struct GroupByIter<'a, E: 'a, F: 'a, G: Eq + 'a> where F: Fn(&'a E) -> G {
- iter: Iter<'a, E>,
- test: Box<F>,
- curr: Option<G>,
- last: Option<&'a E>
- }
- impl<'a, E: 'a, F: 'a, G: Eq + 'a> GroupByIter<'a, E, F, G> where F: Fn(&'a E) -> G {
- fn new(slice: &'a [E], test: F) -> GroupByIter<'a, E, F, G> {
- GroupByIter { iter: slice.iter(), test: Box::new(test), curr: None, last: None }
- }
- }
- pub trait GroupBy {
- type Item;
- fn group_by<'a, F: 'a, G: Eq + 'a>(&'a self, test: F) -> GroupByIter<'a, Self::Item, F, G> where Self::Item: 'a, F: Fn(&'a Self::Item) -> G;
- }
- impl<E> GroupBy for [E] {
- type Item = E;
- fn group_by<'a, F: 'a, G: Eq + 'a>(&'a self, test: F) -> GroupByIter<'a, E, F, G> where E: 'a, F: Fn(&'a E) -> G {
- GroupByIter::new(self, test)
- }
- }
- impl<'a, E: 'a, F: 'a, G: Eq + 'a> Iterator for GroupByIter<'a, E, F, G> where F: Fn(&'a E) -> G {
- type Item = Vec<&'a E>;
- fn next(&mut self) -> Option<Self::Item> {
- let mut group = Vec::new();
- if let Some(e) = self.last {
- group.push(e);
- self.last = None;
- };
- let test = &self.test;
- loop {
- match self.iter.next() {
- None => break,
- Some(e) => {
- let new = Some(test(e));
- if self.curr == new || self.curr.is_none() {
- group.push(e);
- self.curr = new;
- } else {
- self.last = Some(e);
- self.curr = new;
- break;
- }
- }
- }
- };
- if !group.is_empty() { Some(group) } else { None }
- }
- }
- pub trait Countable {
- type Item: Eq + Hash;
- fn counts<'a>(&'a self) -> HashMap<&'a Self::Item, usize>;
- }
- impl<E: Eq + Hash> Countable for [E] {
- type Item = E;
- fn counts<'a>(&'a self) -> HashMap<&'a E, usize> {
- let mut map = HashMap::new();
- for e in self {
- map.insert(e, map.get(e).unwrap_or(&0) + 1);
- }
- map
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
-
⭐⭐⭐Make $1500 in 20 minutes⭐⭐
Java | 9 sec ago | 0.10 KB
-
⭐⭐⭐Make $1500 in 20 minutes⭐⭐
Java | 21 sec ago | 0.10 KB
-
⭐⭐⭐Exploit 500$ in 15 Minutes⭐⭐
Java | 33 sec ago | 0.10 KB
-
⭐⭐⭐MAKE $9OO INSTANTLY D A⭐⭐
Java | 36 sec ago | 0.10 KB
-
⭐⭐⭐MAKE $9OO INSTANTLY D M⭐⭐
Java | 44 sec ago | 0.10 KB
-
⭐⭐⭐Make $1500 in 20 minutes⭐⭐
Java | 46 sec ago | 0.10 KB
-
⭐⭐⭐Profit Method⭐⭐
Java | 55 sec ago | 0.10 KB
-
⭐⭐⭐Exploit 500$ in 15 Minutes⭐⭐
Java | 57 sec ago | 0.10 KB
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand