All current work
This commit is contained in:
@@ -0,0 +1 @@
|
||||
/target
|
||||
Generated
+7
@@ -0,0 +1,7 @@
|
||||
# This file is automatically @generated by Cargo.
|
||||
# It is not intended for manual editing.
|
||||
version = 4
|
||||
|
||||
[[package]]
|
||||
name = "rust-training-fold-custumers"
|
||||
version = "0.1.0"
|
||||
@@ -0,0 +1,6 @@
|
||||
[package]
|
||||
name = "rust-training-fold-custumers"
|
||||
version = "0.1.0"
|
||||
edition = "2024"
|
||||
|
||||
[dependencies]
|
||||
@@ -0,0 +1,35 @@
|
||||
use std::{collections::HashMap};
|
||||
|
||||
struct Order {
|
||||
customer: String,
|
||||
price: u32,
|
||||
paid: bool,
|
||||
}
|
||||
|
||||
fn main() {
|
||||
println!("Hello, world!");
|
||||
|
||||
let orders = vec![
|
||||
Order { customer: "Alice".into(), price: 1200, paid: true },
|
||||
Order { customer: "Bob".into(), price: 500, paid: false },
|
||||
Order { customer: "Alice".into(), price: 800, paid: true },
|
||||
Order { customer: "Charlie".into(), price: 2000, paid: true },
|
||||
Order { customer: "Bob".into(), price: 1500, paid: true },
|
||||
Order { customer: "Alice".into(), price: 300, paid: false },
|
||||
];
|
||||
|
||||
let unique = orders.iter().fold(HashMap::new(), |mut acc, x| {
|
||||
if x.paid {
|
||||
*acc.entry(&x.customer).or_insert(0)+=x.price;
|
||||
}
|
||||
acc
|
||||
});
|
||||
|
||||
let result = unique.iter().fold(String::new(), |mut acc, (x,y)| {
|
||||
acc += &format!("{} {}\n", x, y);
|
||||
acc
|
||||
});
|
||||
|
||||
println!("{}",result);
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user