“BLITZEN! GET IN HERE!” Santa’s furious voice echoed through the workshop.
Blitzen stepped inside cautiously. It had been only a few hours since Santa forgave him for the "great grep fiasco"—when Blitzen had decided to re-write grep
from scratch.
“I thought we were good now!” Blitzen said.
“Well, WE’RE NOT!” Santa shouted, spinning his monitor around. “LOOK AT THIS!”
Blitzen squinted at Santa’s inbox, now overflowing with spam emails:
“What happened?” Blitzen asked.
“I LEAKED MY EMAIL ON TWITCH!” Santa bellowed. “I was streaming my lecture on why Rust traits are better than cookies when I accidentally typed my real address live on stream!”
The chat, of course, had gone wild:
Chat: “LMAO! Bro just doxxed himself live.” “The spam bots are already in his inbox.”
“And it's all because of YOU!” Santa continued.
“ME? How is this my fault?” Blitzen asked, bewildered.
“If you hadn’t wasted the morning re-writing grep
, you’d have caught this issue before it happened!” Santa snapped, slamming his candy cane onto the desk. “Now you’re going to fix it. Write me an API that anonymizes email addresses—Christmas style. Replace the local part with festive emojis, and make sure it doesn’t crash on invalid emails. Do it NOW!”
Blitzen sighed and opened Vim. “Okay, okay… I’m on it.”
Blitzen as always is in trouble—again.
Here's what you gotta do to help him out:
[email protected]
should be anonymized to 🎅🎄🎁🎄🎅@north.pole
.santa
should be anonymized to 🎅🎄🎁🎄🎅
.Here's how Santa likes to use this API:
fn main() {
let email = "[email protected]";
let anonymized = email.anonymize_email();
}
Figure out a way to make this work, otherwise Blitzen will not get his cookies this Christmas!
If you're stuck or need a starting point, here are some hints to help you along the way!
You can extend the String
type by implementing a trait for it.
Define a trait named Anonymize
with a method named anonymize_email
that returns a String
.
pub trait Anonymize {
fn anonymize_email(&self) -> String;
}
Implement the trait for the String
type.
impl Anonymize for String {
fn anonymize_email(&self) -> String {
// Implement the method
}
}
// Ensure all relevant items are marked with `pub` keyworduse std::error::Error;const CHRISTMAS_EMOJIS: [char; 4] = ['🎅', '🤶', '🎄', '🎁'];// Your Solution here ...pub trait Anonymize { fn anonymize_email(&self) -> String;}pub struct Emailparts<'a> { pub parts: Option<&'a str>, pub is_valid: bool}pub fn is_valid(email: &String) -> Result<Emailparts<'_>, Box<dyn Error>> { let parts_separated_at: Vec<&str> = email.split("@").collect(); if !parts_separated_at.is_empty() { match parts_separated_at.len() { 1 => { return Ok(Emailparts { parts: Some(parts_separated_at[0]), is_valid: false })}, 2 => { return Ok(Emailparts { parts: Some(parts_separated_at[0]), is_valid: true })}, _ => { return Ok(Emailparts { parts: None, is_valid: false })} } } Ok(Emailparts { parts: None, is_valid: false })}impl Anonymize for String { fn anonymize_email(&self) -> String { println!("to anonymize {}",self ); if let Ok(email_parts) = is_valid(self) { if email_parts.is_valid { // Valid email: replace characters in local part only let local_parts = email_parts.parts.unwrap(); let domain = self.split("@").nth(1).unwrap_or(""); // let mut res_arr = Vec::new(); // for part in local_parts { // let mut anonymized_local = String::new(); // for (i, _char) in part.chars().enumerate() { // let emoji_index = i % CHRISTMAS_EMOJIS.len(); // anonymized_local.push(CHRISTMAS_EMOJIS[emoji_index]); // } // res_arr.push(anonymized_local); // } let mut anonymized_local = String::new(); for (i, _char) in local_parts.chars().enumerate() { let emoji_index = i % CHRISTMAS_EMOJIS.len(); anonymized_local.push(CHRISTMAS_EMOJIS[emoji_index]); } // let res = res_arr.join(""); let ans = format!("{}@{}", anonymized_local, domain); println!("After anony: {}", ans); return ans; } else { // let local_parts: Vec<&str> = email_parts.parts.unwrap().split(".") // .filter(|p| !p.is_empty()) // .collect(); let local_parts = email_parts.parts.unwrap(); // let domain = self.split("@").nth(1).unwrap_or(""); // let mut res_arr = Vec::new(); // for part in local_parts { // let mut anonymized_local = String::new(); // for (i, _char) in part.chars().enumerate() { // let emoji_index = i % CHRISTMAS_EMOJIS.len(); // anonymized_local.push(CHRISTMAS_EMOJIS[emoji_index]); // } // res_arr.push(anonymized_local); // } let mut anonymized_local = String::new(); for (i, _char) in local_parts.chars().enumerate() { let emoji_index = i % CHRISTMAS_EMOJIS.len(); anonymized_local.push(CHRISTMAS_EMOJIS[emoji_index]); } // let res = res_arr.join(""); let ans = format!("{}", anonymized_local); println!("After anony: {}", ans); return ans; } } "".to_string() }}pub fn main() { let emails = vec![ "[email protected]".to_string(), "[email protected]".to_string(), "[email protected]".to_string(), "[email protected]".to_string(), "[email protected]".to_string(), "[email protected]".to_string(), "[email protected]".to_string(), "[email protected]".to_string(), "[email protected]".to_string(), "[email protected]".to_string(), ]; for email in emails { let anonymized_email = email.anonymize_email(); // This is the API that Santa wants! println!("Original: {} -> Anonymized: {}", email, anonymized_email); }}
// Ensure all relevant items are marked with `pub` keywordconst CHRISTMAS_EMOJIS: [char; 4] = ['🎅', '🤶', '🎄', '🎁'];// Your Solution here ...pub trait Anonymized { fn anonymize_email(&self) -> String;}impl Anonymized for String { fn anonymize_email(&self) -> String { let change_until_index = self.chars() .position(|c| c == '@') .unwrap_or(self.len()); self.chars().enumerate().map(|(i, c)| { if i < change_until_index { CHRISTMAS_EMOJIS[i % 4] } else { c } }) .collect() }}pub fn main() { let emails = vec![ "[email protected]".to_string(), "[email protected]".to_string(), "[email protected]".to_string(), "[email protected]".to_string(), "[email protected]".to_string(), "[email protected]".to_string(), "[email protected]".to_string(), "[email protected]".to_string(), "[email protected]".to_string(), "[email protected]".to_string(), ]; for email in emails { let anonymized_email = email.anonymize_email(); // This is the API that Santa wants! println!("Original: {} -> Anonymized: {}", email, anonymized_email); }}
// Ensure all relevant items are marked with `pub` keywordpub trait Anonymize { fn anonymize_email(&self) -> String;}const CHRISTMAS_EMOJIS: [char; 4] = ['🎅', '🤶', '🎄', '🎁'];// Your Solution here ...impl Anonymize for String { fn anonymize_email(&self) -> String { let mut res = Vec::new(); let mut emojis = CHRISTMAS_EMOJIS.iter().cycle(); let chars: Vec<char> = self.chars().collect(); for (i, c) in chars.iter().enumerate() { if *c == '@' { res.extend_from_slice(&chars[i..]); break; } else { if let Some(e) = emojis.next() { res.push(*e); } } } res.iter().collect() }}pub fn main() { let emails = vec![ "[email protected]".to_string(), "[email protected]".to_string(), "[email protected]".to_string(), "[email protected]".to_string(), "[email protected]".to_string(), "[email protected]".to_string(), "[email protected]".to_string(), "[email protected]".to_string(), "[email protected]".to_string(), "[email protected]".to_string(), ]; for email in emails { let anonymized_email = email.anonymize_email(); // This is the API that Santa wants! println!("Original: {} -> Anonymized: {}", email, anonymized_email); }}
// Ensure all relevant items are marked with `pub` keywordconst CHRISTMAS_EMOJIS: [char; 4] = ['🎅', '🤶', '🎄', '🎁'];pub trait Anonymize { fn anonymize_email(&self) -> String;}impl Anonymize for String { fn anonymize_email(&self) -> String { let mut local = true; let mut emojis = std::iter::repeat(CHRISTMAS_EMOJIS).flatten(); self.chars() .map(|c| { if c == '@' { local = false; } if local { emojis.next().unwrap() } else { c } }) .collect() }}pub fn main() { let emails = vec![ "[email protected]".to_string(), "[email protected]".to_string(), "[email protected]".to_string(), "[email protected]".to_string(), "[email protected]".to_string(), "[email protected]".to_string(), "[email protected]".to_string(), "[email protected]".to_string(), "[email protected]".to_string(), "[email protected]".to_string(), ]; for email in emails { let anonymized_email = email.anonymize_email(); // This is the API that Santa wants! println!("Original: {} -> Anonymized: {}", email, anonymized_email); }}
// Ensure all relevant items are marked with `pub` keywordconst CHRISTMAS_EMOJIS: [char; 4] = ['🎅', '🤶', '🎄', '🎁'];// Your Solution here ...pub trait AnonymizeEmail { fn anonymize_email(&self) -> String;}impl AnonymizeEmail for String { fn anonymize_email(&self) -> String { let mut local = true; let mut emojis = std::iter::repeat(CHRISTMAS_EMOJIS).flatten(); self.chars().map(|c| { if c == '@' { local = false; } if local {emojis.next().unwrap()} else {c} }).collect() }}pub fn main() { let emails = vec![ "[email protected]".to_string(), "[email protected]".to_string(), "[email protected]".to_string(), "[email protected]".to_string(), "[email protected]".to_string(), "[email protected]".to_string(), "[email protected]".to_string(), "[email protected]".to_string(), "[email protected]".to_string(), "[email protected]".to_string(), ]; for email in emails { let anonymized_email = email.anonymize_email(); // This is the API that Santa wants! println!("Original: {} -> Anonymized: {}", email, anonymized_email); }}
// Ensure all relevant items are marked with `pub` keywordconst CHRISTMAS_EMOJIS: [char; 4] = ['🎅', '🤶', '🎄', '🎁'];// Your Solution here ...pub trait Anonymize { fn anonymize_email(&self) -> String;}impl Anonymize for String { fn anonymize_email(&self) -> Self { // Find last index until when emojis shall be applied let last_index = self.find("@").unwrap_or(self.len()); // Apply emojis let anonymized = self.clone(); anonymized .chars() .enumerate() .map(|(idx, c)| { if idx < last_index { CHRISTMAS_EMOJIS[idx % CHRISTMAS_EMOJIS.len()] } else { c } }) .collect() }}pub fn main() { let emails = vec![ "[email protected]".to_string(), "[email protected]".to_string(), "[email protected]".to_string(), "[email protected]".to_string(), "[email protected]".to_string(), "[email protected]".to_string(), "[email protected]".to_string(), "[email protected]".to_string(), "[email protected]".to_string(), "[email protected]".to_string(), ]; for email in emails { let anonymized_email = email.anonymize_email(); // This is the API that Santa wants! println!("Original: {} -> Anonymized: {}", email, anonymized_email); }}
// Ensure all relevant items are marked with `pub` keywordconst CHRISTMAS_EMOJIS: [char; 4] = ['🎅', '🤶', '🎄', '🎁'];pub trait Anonymize { fn anonymize_email(&self) -> String;}pub fn get_mask(mask_len: usize) -> String { (0..mask_len).map(|idx| CHRISTMAS_EMOJIS[idx % CHRISTMAS_EMOJIS.len()]).collect()}impl Anonymize for String { fn anonymize_email(&self) -> String { match self.split_once('@') { Some((_email, domain)) => { format!("{}@{}", get_mask(_email.chars().count()), domain) } _ => { get_mask(self.chars().count()) } } }}// Your Solution here ...pub fn main() { let emails = vec![ "[email protected]".to_string(), "[email protected]".to_string(), "[email protected]".to_string(), "[email protected]".to_string(), "[email protected]".to_string(), "[email protected]".to_string(), "[email protected]".to_string(), "[email protected]".to_string(), "[email protected]".to_string(), "[email protected]".to_string(), ]; for email in emails { let anonymized_email = email.anonymize_email(); // This is the API that Santa wants! println!("Original: {} -> Anonymized: {}", email, anonymized_email); }}
// Ensure all relevant items are marked with `pub` keywordconst CHRISTMAS_EMOJIS: [char; 4] = ['🎅', '🤶', '🎄', '🎁'];pub trait Anonymize { fn anonymize_email(&self) -> String;}impl Anonymize for String { fn anonymize_email(&self) -> String { let position = self.find('@'); let (local, domain) = match position { Some(pos) => { let (l, d) = self.split_at(pos); (l, d) }, None => (self.as_str(), ""), }; let result: String = local .chars() .enumerate() .map(|(i, _)| CHRISTMAS_EMOJIS[i % 4]) .collect(); format!("{result}{domain}") }}pub fn main() { let emails = vec![ "[email protected]".to_string(), "[email protected]".to_string(), "[email protected]".to_string(), "[email protected]".to_string(), "[email protected]".to_string(), "[email protected]".to_string(), "[email protected]".to_string(), "[email protected]".to_string(), "[email protected]".to_string(), "[email protected]".to_string(), ]; for email in emails { let anonymized_email = email.anonymize_email(); // This is the API that Santa wants! println!("Original: {} -> Anonymized: {}", email, anonymized_email); }}
// Ensure all relevant items are marked with `pub` keywordconst CHRISTMAS_EMOJIS: [char; 4] = ['🎅', '🤶', '🎄', '🎁'];pub trait Anonymize { fn anonymize_email(&self) -> String;}impl Anonymize for String { fn anonymize_email(&self) -> String { let mut anonymized_email = String::new(); for (i, c) in self.chars().enumerate() { if c == '@' { anonymized_email.push_str(&self[i..]); break; } else { anonymized_email.push(CHRISTMAS_EMOJIS[i as usize % 4]); } } anonymized_email }}pub fn main() { let emails = vec![ "[email protected]".to_string(), "[email protected]".to_string(), "[email protected]".to_string(), "[email protected]".to_string(), "[email protected]".to_string(), "[email protected]".to_string(), "[email protected]".to_string(), "[email protected]".to_string(), "[email protected]".to_string(), "[email protected]".to_string(), ]; for email in emails { let anonymized_email = email.anonymize_email(); // This is the API that Santa wants! println!("Original: {} -> Anonymized: {}", email, anonymized_email); }}
// Ensure all relevant items are marked with `pub` keywordconst CHRISTMAS_EMOJIS: [char; 4] = ['🎅', '🤶', '🎄', '🎁'];pub trait Anonymize{ fn anonymize_email(&self) -> String; }impl Anonymize for String { fn anonymize_email(&self) -> String{ if let Some((private, public)) = self.split_once("@"){ let anonymized: String = private.bytes().map(|b| CHRISTMAS_EMOJIS[(b as usize) % 4]).collect(); anonymized + "@" + public } else { self.bytes().map(|b| CHRISTMAS_EMOJIS[(b as usize) % 4]).collect() } }}pub fn main() { let emails = vec![ "[email protected]".to_string(), "[email protected]".to_string(), "[email protected]".to_string(), "[email protected]".to_string(), "[email protected]".to_string(), "[email protected]".to_string(), "[email protected]".to_string(), "[email protected]".to_string(), "[email protected]".to_string(), "[email protected]".to_string(), ]; for email in emails { let anonymized_email = email.anonymize_email(); // This is the API that Santa wants! println!("Original: {} -> Anonymized: {}", email, anonymized_email); }}
// Ensure all relevant items are marked with `pub` keywordconst CHRISTMAS_EMOJIS: [char; 4] = ['🎅', '🤶', '🎄', '🎁'];// Your Solution here ...pub trait Anonymize { fn anonymize_email(&self) -> String;}impl Anonymize for String { fn anonymize_email(&self) -> String { let mut result = String::new(); let mut emojis = CHRISTMAS_EMOJIS.iter().cycle(); let mut it = self.chars(); for c in it.by_ref() { if c == '@' { result.push(c); break; } result.push(*emojis.next().unwrap_or(&'_')); } result.extend(it); result }}pub fn main() { let emails = vec![ "[email protected]".to_string(), "[email protected]".to_string(), "[email protected]".to_string(), "[email protected]".to_string(), "[email protected]".to_string(), "[email protected]".to_string(), "[email protected]".to_string(), "[email protected]".to_string(), "[email protected]".to_string(), "[email protected]".to_string(), ]; for email in emails { let anonymized_email = email.anonymize_email(); // This is the API that Santa wants! println!("Original: {} -> Anonymized: {}", email, anonymized_email); }}#[cfg(test)]mod tests { use super::*; #[test] fn test_name() { main(); }}
// Ensure all relevant items are marked with `pub` keywordconst CHRISTMAS_EMOJIS: [char; 4] = ['🎅', '🤶', '🎄', '🎁'];pub trait Anonymizer { fn anonymize_email(&self) -> String;}impl Anonymizer for String { fn anonymize_email(&self) -> String { let mut result = String::new(); let mut iter = self.chars(); let mut index = 0; while let Some(ch) = iter.next() { if ch == '@' { result.push(ch); result.extend(iter); break; } else { result.push(CHRISTMAS_EMOJIS[index]); index = (index + 1) % CHRISTMAS_EMOJIS.len(); } } result }}pub fn main() { let emails = vec![ "[email protected]".to_string(), "[email protected]".to_string(), "[email protected]".to_string(), "[email protected]".to_string(), "[email protected]".to_string(), "[email protected]".to_string(), "[email protected]".to_string(), "[email protected]".to_string(), "[email protected]".to_string(), "[email protected]".to_string(), ]; for email in emails { let anonymized_email = email.anonymize_email(); // This is the API that Santa wants! println!("Original: {} -> Anonymized: {}", email, anonymized_email); }}
// Ensure all relevant items are marked with `pub` keywordconst CHRISTMAS_EMOJIS: [char; 4] = ['🎅', '🤶', '🎄', '🎁'];// Your Solution here ...pub trait Anonymize { fn anonymize_email(&self) -> String;}impl Anonymize for String { fn anonymize_email(&self) -> String { if self.chars().filter(|&c| c == '@').count() == 1 { let mut iter = self.split('@'); let local = iter.next() .unwrap() .to_string(); let domain = iter.next() .unwrap() .to_string(); let anon: String = (0..local.len()).map(|idx| { CHRISTMAS_EMOJIS[idx % CHRISTMAS_EMOJIS.len()] }) .collect(); format!("{anon}@{domain}") } else { self.chars() .map(|ch| { CHRISTMAS_EMOJIS[ch as usize % CHRISTMAS_EMOJIS.len()] }) .collect() } }} pub fn main() { let emails = vec![ "[email protected]".to_string(), "[email protected]".to_string(), "[email protected]".to_string(), "[email protected]".to_string(), "[email protected]".to_string(), "[email protected]".to_string(), "[email protected]".to_string(), "[email protected]".to_string(), "[email protected]".to_string(), "[email protected]".to_string(), ]; for email in emails { let anonymized_email = email.anonymize_email(); // This is the API that Santa wants! println!("Original: {} -> Anonymized: {}", email, anonymized_email); }}
// Ensure all relevant items are marked with `pub` keywordconst CHRISTMAS_EMOJIS: [char; 4] = ['🎅', '🤶', '🎄', '🎁'];pub trait Anonymize { fn anonymize_email(&self) -> String;}impl Anonymize for String { fn anonymize_email(&self) -> String { let mut found_at = false; self.chars() .enumerate() .map(|(i, c)| { if c == '@' { found_at = true } if found_at { c } else { CHRISTMAS_EMOJIS[i % 4] } }) .collect() }}pub fn main() { let emails = vec![ "[email protected]".to_string(), "[email protected]".to_string(), "[email protected]".to_string(), "[email protected]".to_string(), "[email protected]".to_string(), "[email protected]".to_string(), "[email protected]".to_string(), "[email protected]".to_string(), "[email protected]".to_string(), "[email protected]".to_string(), ]; for email in emails { let anonymized_email = email.anonymize_email(); // This is the API that Santa wants! println!("Original: {} -> Anonymized: {}", email, anonymized_email); }}
// Ensure all relevant items are marked with `pub` keywordconst CHRISTMAS_EMOJIS: [char; 4] = ['🎅', '🤶', '🎄', '🎁'];// Your Solution here ...pub trait Email { fn anonymize_email(&self) -> String;}impl Email for String { fn anonymize_email(&self) -> String { if self.chars().filter(|&chr| chr == '@').count() == 1 { let mut split = self.split("@"); let local = split.next().unwrap(); let ending = split.next().unwrap(); let anonymous: String = (0..local.len()).map(|idx| CHRISTMAS_EMOJIS[idx % 4]).collect(); format!("{anonymous}@{ending}") } else { let string = (0..self.len()).map(|idx| CHRISTMAS_EMOJIS[idx % 4]).collect(); string } }}pub fn main() { let emails = vec![ "[email protected]".to_string(), "[email protected]".to_string(), "[email protected]".to_string(), "[email protected]".to_string(), "[email protected]".to_string(), "[email protected]".to_string(), "[email protected]".to_string(), "[email protected]".to_string(), "[email protected]".to_string(), "[email protected]".to_string(), ]; for email in emails { let anonymized_email = email.anonymize_email(); // This is the API that Santa wants! println!("Original: {} -> Anonymized: {}", email, anonymized_email); }}
// Ensure all relevant items are marked with `pub` keywordconst CHRISTMAS_EMOJIS: [char; 4] = ['🎅', '🤶', '🎄', '🎁'];// Your Solution here ...pub trait Anonymize { fn anonymize_email(&self) -> String;}impl Anonymize for String { fn anonymize_email(&self) -> String { let parts: Vec<&str> = self.split("@").collect(); if parts.len() != 2 { // invalid email self.chars() .map(|ch| { CHRISTMAS_EMOJIS[(ch as u32 % 4) as usize] }) .collect() } else { let local: String = parts[0].chars() .map(|ch| { CHRISTMAS_EMOJIS[(ch as u32 % 4) as usize] }) .collect(); local + "@" + parts[1] } }}pub fn main() { let emails = vec![ "[email protected]".to_string(), "[email protected]".to_string(), "[email protected]".to_string(), "[email protected]".to_string(), "[email protected]".to_string(), "[email protected]".to_string(), "[email protected]".to_string(), "[email protected]".to_string(), "[email protected]".to_string(), "[email protected]".to_string(), ]; for email in emails { let anonymized_email = email.anonymize_email(); // This is the API that Santa wants! println!("Original: {} -> Anonymized: {}", email, anonymized_email); }}
// Ensure all relevant items are marked with `pub` keywordconst CHRISTMAS_EMOJIS: [char; 4] = ['🎅', '🤶', '🎄', '🎁'];// Your Solution here ...pub trait Anonymize { fn anonymize_email(&self) -> String;}pub fn decorate(text: &str) -> String { text.chars() .enumerate() .map(|(i, _ch)| CHRISTMAS_EMOJIS[i % CHRISTMAS_EMOJIS.len()]) .collect()}impl Anonymize for String { fn anonymize_email(&self) -> String { let parts: Vec<&str> = self.split('@').collect(); if parts.len() != 2 { return decorate(self); } let (first_part, end_part) = (parts[0], parts[1]); let mut decorated = decorate(first_part); decorated.push('@'); decorated.push_str(end_part); decorated }}
// Ensure all relevant items are marked with `pub` keywordconst CHRISTMAS_EMOJIS: [char; 4] = ['🎅', '🤶', '🎄', '🎁'];// Your Solution here ...pub trait Anonymus { fn anonymize_email(&self) -> Self;}impl Anonymus for String { fn anonymize_email(&self) -> String { self.chars() .take_while(|&c| c != '@') .map(|i| CHRISTMAS_EMOJIS[(i as usize) % CHRISTMAS_EMOJIS.len()]) .collect() }}pub fn main() { let emails = vec![ "[email protected]".to_string(), "[email protected]".to_string(), "[email protected]".to_string(), "[email protected]".to_string(), "[email protected]".to_string(), "[email protected]".to_string(), "[email protected]".to_string(), "[email protected]".to_string(), "[email protected]".to_string(), "[email protected]".to_string(), ]; for email in emails { let anonymized_email = email.anonymize_email(); // This is the API that Santa wants! println!("Original: {} -> Anonymized: {}", email, anonymized_email); }}
const CHRISTMAS_EMOJIS: [char; 4] = ['🎅', '🤶', '🎄', '🎁'];pub trait Anonymizer { fn anonymize_email(&self) -> String;}impl Anonymizer for String { fn anonymize_email(&self) -> String { let mut result = String::new(); let mut iter = self.chars(); let mut index = 0; while let Some(ch) = iter.next() { if ch == '@' { result.push(ch); result.extend(iter); break; } else { result.push(CHRISTMAS_EMOJIS[index]); index = (index + 1) % CHRISTMAS_EMOJIS.len(); } } result }}pub fn main() { let emails = vec![ "[email protected]".to_string(), "[email protected]".to_string(), "[email protected]".to_string(), "[email protected]".to_string(), "[email protected]".to_string(), "[email protected]".to_string(), "[email protected]".to_string(), "[email protected]".to_string(), "[email protected]".to_string(), "[email protected]".to_string(), ]; for email in emails { let anonymized_email = email.anonymize_email(); // This is the API that Santa wants! println!("Original: {} -> Anonymized: {}", email, anonymized_email); }}
// Ensure all relevant items are marked with `pub` keywordconst CHRISTMAS_EMOJIS: [char; 4] = ['🎅', '🤶', '🎄', '🎁'];// Your Solution here ...pub trait Anonymized { fn anonymize_email(&self) -> String; }impl Anonymized for String { fn anonymize_email(&self) -> String { self.chars().take_while(|&c| c!= '@').map(|i| CHRISTMAS_EMOJIS[(i as usize)% CHRISTMAS_EMOJIS.len()]).collect() }}pub fn main() { let emails = vec![ "[email protected]".to_string(), "[email protected]".to_string(), "[email protected]".to_string(), "[email protected]".to_string(), "[email protected]".to_string(), "[email protected]".to_string(), "[email protected]".to_string(), "[email protected]".to_string(), "[email protected]".to_string(), "[email protected]".to_string(), ]; for email in emails { let anonymized_email = email.anonymize_email(); // This is the API that Santa wants! println!("Original: {} -> Anonymized: {}", email, anonymized_email); }}
// Ensure all relevant items are marked with `pub` keywordconst CHRISTMAS_EMOJIS: [char; 4] = ['🎅', '🤶', '🎄', '🎁'];// Your Solution here ...pub trait AnonymizeEmail { fn anonymize_email(&self) -> String; }impl AnonymizeEmail for String { fn anonymize_email(&self) -> String { let mut result = String::new(); let mut cur = 0; let mut i = self.chars(); let mut ch = i.next(); loop { if let Some(c) = ch { if c == '@' { result.push(c); break; } result.push(CHRISTMAS_EMOJIS[cur]); cur = (cur + 1) % CHRISTMAS_EMOJIS.len(); } else { break; } ch = i.next(); } while let Some(ch) = i.next() { result.push(ch); } result }}pub fn main() { let emails = vec![ "[email protected]".to_string(), "[email protected]".to_string(), "[email protected]".to_string(), "[email protected]".to_string(), "[email protected]".to_string(), "[email protected]".to_string(), "[email protected]".to_string(), "[email protected]".to_string(), "[email protected]".to_string(), "[email protected]".to_string(), ]; for email in emails { let anonymized_email = email.anonymize_email(); // This is the API that Santa wants! println!("Original: {} -> Anonymized: {}", email, anonymized_email); }}
// Ensure all relevant items are marked with `pub` keywordconst CHRISTMAS_EMOJIS: [char; 4] = ['🎅', '🤶', '🎄', '🎁'];pub trait Anonymize { fn anonymize_email(&self) -> String;}impl Anonymize for String { fn anonymize_email(&self) -> String { self.chars() .into_iter() .take_while(|&c| c!='@') .enumerate() .map(|(i, _)| CHRISTMAS_EMOJIS[i % CHRISTMAS_EMOJIS.len()]) .collect() }}pub fn main() { let emails = vec![ "[email protected]".to_string(), "[email protected]".to_string(), "[email protected]".to_string(), "[email protected]".to_string(), "[email protected]".to_string(), "[email protected]".to_string(), "[email protected]".to_string(), "[email protected]".to_string(), "[email protected]".to_string(), "[email protected]".to_string(), ]; for email in emails { let anonymized_email = email.anonymize_email(); // This is the API that Santa wants! println!("Original: {} -> Anonymized: {}", email, anonymized_email); }}
// Ensure all relevant items are marked with `pub` keywordconst CHRISTMAS_EMOJIS: [char; 4] = ['🎅', '🤶', '🎄', '🎁'];// Your Solution here ...pub trait Anonymize { fn anonymize_email(&self) -> String;}impl Anonymize for String { fn anonymize_email(&self) -> String { self.chars() .take_while(|&c| c != '@') .map(|c| CHRISTMAS_EMOJIS[(c as usize) % CHRISTMAS_EMOJIS.len()]) .collect() }}pub fn main() { let emails = vec![ "[email protected]".to_string(), "[email protected]".to_string(), "[email protected]".to_string(), "[email protected]".to_string(), "[email protected]".to_string(), "[email protected]".to_string(), "[email protected]".to_string(), "[email protected]".to_string(), "[email protected]".to_string(), "[email protected]".to_string(), ]; for email in emails { let anonymized_email = email.anonymize_email(); // This is the API that Santa wants! println!("Original: {} -> Anonymized: {}", email, anonymized_email); }}
// Ensure all relevant items are marked with `pub` keywordconst CHRISTMAS_EMOJIS: [char; 4] = ['🎅', '🤶', '🎄', '🎁'];// Your Solution here ...// Maybe best would be an email struct, e.g. as a newtype:// struct Email(String)// ...but if we want to keep things exactly as Santa wants, need an extension trait.// Convention is to add Ext to end of trait name for extension traits.pub trait EmailExt { fn anonymize_email(&self) -> String;}impl EmailExt for String { fn anonymize_email(&self) -> String { // If there is no @, this Split will contain the whole string let mut email_split = self.split('@'); // If the String is empty, the len will be 0 and an empty anonymised string will be returned. // This is reasonable, since there's no way for this API to return an error. let username_len = email_split.next().unwrap().chars().count(); let mut anon_email = String::new(); for i in 0..username_len { anon_email.push(CHRISTMAS_EMOJIS[i % CHRISTMAS_EMOJIS.len()]); } // Add domain name, if present match email_split.next() { Some(domain_name) => { anon_email.push('@'); anon_email.push_str(domain_name); anon_email }, None => anon_email } }}pub fn main() { let emails = vec![ "[email protected]".to_string(), "[email protected]".to_string(), "[email protected]".to_string(), "[email protected]".to_string(), "[email protected]".to_string(), "[email protected]".to_string(), "[email protected]".to_string(), "[email protected]".to_string(), "[email protected]".to_string(), "[email protected]".to_string(), ]; for email in emails { let anonymized_email = email.anonymize_email(); // This is the API that Santa wants! println!("Original: {} -> Anonymized: {}", email, anonymized_email); }}
// Ensure all relevant items are marked with `pub` keywordconst CHRISTMAS_EMOJIS: [char; 4] = ['🎅', '🤶', '🎄', '🎁'];// Your Solution here ...pub trait Anonymizer { fn anonymize_email(&self) -> String;}impl Anonymizer for String { fn anonymize_email(&self) -> String { let index = match self.chars().position(|c| c == '@') { Some(index) => index, None => self.len(), }; let replacement: String = self[0..index] .chars() .enumerate() .map(|(index, _)| CHRISTMAS_EMOJIS[index % CHRISTMAS_EMOJIS.len()]) .collect(); let mut anon_string = self.clone(); anon_string.replace_range(0..index, &replacement); anon_string }}pub fn main() { let emails = vec![ "[email protected]".to_string(), "[email protected]".to_string(), "[email protected]".to_string(), "[email protected]".to_string(), "[email protected]".to_string(), "[email protected]".to_string(), "[email protected]".to_string(), "[email protected]".to_string(), "[email protected]".to_string(), "[email protected]".to_string(), "existential.crisis".to_string(), "".to_string(), ]; for email in emails { let anonymized_email = email.anonymize_email(); // This is the API that Santa wants! println!("Original: {} -> Anonymized: {}", email, anonymized_email); }}
// Ensure all relevant items are marked with `pub` keywordconst CHRISTMAS_EMOJIS: [char; 4] = ['🎅', '🤶', '🎄', '🎁'];// Your Solution here ...pub trait Anonymizer { fn anonymize_email(&self) -> String;}impl Anonymizer for String { fn anonymize_email(&self) -> String { let index = match self.chars().position(|c| c == '@') { Some(index) => index, None => self.len(), }; let replacement: String = self[0..index] .chars() .enumerate() .map(|(index, _)| CHRISTMAS_EMOJIS[index % CHRISTMAS_EMOJIS.len()]) .collect(); let mut anon_string = self.clone(); anon_string.replace_range(0..index, &replacement); anon_string }}pub fn main() { let emails = vec![ "[email protected]".to_string(), "[email protected]".to_string(), "[email protected]".to_string(), "[email protected]".to_string(), "[email protected]".to_string(), "[email protected]".to_string(), "[email protected]".to_string(), "[email protected]".to_string(), "[email protected]".to_string(), "[email protected]".to_string(), "existential.crisis".to_string(), "".to_string(), ]; for email in emails { let anonymized_email = email.anonymize_email(); // This is the API that Santa wants! println!("Original: {} -> Anonymized: {}", email, anonymized_email); }}
// Ensure all relevant items are marked with `pub` keywordconst CHRISTMAS_EMOJIS: [char; 4] = ['🎅', '🤶', '🎄', '🎁'];// Your Solution here ...pub trait AnonymizableEmail { fn anonymize_email(&self) -> String;}pub fn emojify(str: &str) -> String { str.chars().enumerate().map(|(i, _)| CHRISTMAS_EMOJIS[i % CHRISTMAS_EMOJIS.len()]).collect()}impl AnonymizableEmail for String { fn anonymize_email(&self) -> String { if let Some(at_index) = self.find('@') { format!("{}{}", emojify(&self[0..at_index]), &self[at_index..]) } else { emojify(&self) } }}pub fn main() { let emails = vec![ "[email protected]".to_string(), "[email protected]".to_string(), "[email protected]".to_string(), "[email protected]".to_string(), "[email protected]".to_string(), "[email protected]".to_string(), "[email protected]".to_string(), "[email protected]".to_string(), "[email protected]".to_string(), "[email protected]".to_string(), ]; for email in emails { let anonymized_email = email.anonymize_email(); // This is the API that Santa wants! println!("Original: {} -> Anonymized: {}", email, anonymized_email); }}
// Ensure all relevant items are marked with `pub` keywordconst CHRISTMAS_EMOJIS: [char; 4] = ['🎅', '🤶', '🎄', '🎁'];// Your Solution here ...//pub trait Anonymize { fn anonymize_email(&self) -> String;}impl Anonymize for String { fn anonymize_email(&self) -> String { let email_parts: Vec<&str> = self.split('@').collect(); if email_parts.len() != 2 { CHRISTMAS_EMOJIS.iter().cycle().take(self.len()).collect() } else { let username = email_parts[0]; format!( "{}@{}", CHRISTMAS_EMOJIS .iter() .cycle() .take(username.len()) .collect::<String>(), email_parts[1] ) } }}pub fn main() { let emails = vec![ "[email protected]".to_string(), "[email protected]".to_string(), "[email protected]".to_string(), "[email protected]".to_string(), "[email protected]".to_string(), "[email protected]".to_string(), "[email protected]".to_string(), "[email protected]".to_string(), "[email protected]".to_string(), "[email protected]".to_string(), ]; for email in emails { let anonymized_email = email.anonymize_email(); // This is the API that Santa wants! println!("Original: {} -> Anonymized: {}", email, anonymized_email); }}
// Ensure all relevant items are marked with `pub` keywordconst CHRISTMAS_EMOJIS: [char; 4] = ['🎅', '🤶', '🎄', '🎁'];// Your Solution here ...pub trait Anonymize { fn anonymize_email(&self) -> String;}impl Anonymize for String { fn anonymize_email(&self) -> String { let parts = self.split('@').collect::<Vec<_>>(); if parts.len() != 2 { // anonymize whole email CHRISTMAS_EMOJIS .iter() .cycle() .take(self.len()) .collect::<String>() } else { // anonyize only the first part let anonymize_part = CHRISTMAS_EMOJIS .iter() .cycle() .take(parts[0].len()) .collect::<String>(); format!("{}@{}", anonymize_part, parts[1]) } }}pub fn main() { let emails = vec![ "[email protected]".to_string(), "[email protected]".to_string(), "[email protected]".to_string(), "[email protected]".to_string(), "[email protected]".to_string(), "[email protected]".to_string(), "[email protected]".to_string(), "[email protected]".to_string(), "[email protected]".to_string(), "[email protected]".to_string(), ]; for email in emails { let anonymized_email = email.anonymize_email(); // This is the API that Santa wants! println!("Original: {} -> Anonymized: {}", email, anonymized_email); }}
// Ensure all relevant items are marked with `pub` keywordconst CHRISTMAS_EMOJIS: [char; 4] = ['🎅', '🤶', '🎄', '🎁'];// Your Solution here ...pub trait Anonymize { fn anonymize_email(&self) -> String;}impl Anonymize for String { fn anonymize_email(&self) -> String { eprintln!("Email is {self}"); let parts: Vec<&str> = self.split('@').collect(); if parts.len() != 2 { CHRISTMAS_EMOJIS.iter().cycle().take(parts[0].len()).collect() } else { let (local, domain) = (parts[0], parts[1]); let new_local = CHRISTMAS_EMOJIS.iter().cycle().take(local.len()).collect::<String>(); new_local + "@" + domain } }}pub fn main() { let emails = vec![ "[email protected]".to_string(), "[email protected]".to_string(), "[email protected]".to_string(), "[email protected]".to_string(), "[email protected]".to_string(), "[email protected]".to_string(), "[email protected]".to_string(), "[email protected]".to_string(), "[email protected]".to_string(), "[email protected]".to_string(), ]; for email in emails { let anonymized_email = email.anonymize_email(); // This is the API that Santa wants! println!("Original: {} -> Anonymized: {}", email, anonymized_email); }}
// Ensure all relevant items are marked with `pub` keywordconst CHRISTMAS_EMOJIS: [char; 4] = ['🎅', '🤶', '🎄', '🎁'];pub trait AnonymizeEmail { fn anonymize_email(&self) -> String;}impl AnonymizeEmail for String { fn anonymize_email(&self) -> String { // split the email into two parts: before and after the '@' symbol let at_index = self.find('@').unwrap_or(self.len()); let (before_at, suffix) = self.split_at(at_index); // iterate over the string and replace each character with a the next Christmas emoji without randomness let anonymized = before_at .chars() .map(|c| { let index = c.to_ascii_lowercase() as usize % CHRISTMAS_EMOJIS.len(); CHRISTMAS_EMOJIS[index] }) .collect::<String>(); anonymized + suffix}}pub fn main() { let emails = vec![ "[email protected]".to_string(), "[email protected]".to_string(), "[email protected]".to_string(), "[email protected]".to_string(), "[email protected]".to_string(), "[email protected]".to_string(), "[email protected]".to_string(), "[email protected]".to_string(), "[email protected]".to_string(), "[email protected]".to_string(), ]; for email in emails { let anonymized_email = email.anonymize_email(); // This is the API that Santa wants! println!("Original: {} -> Anonymized: {}", email, anonymized_email); }}
// Ensure all relevant items are marked with `pub` keywordconst CHRISTMAS_EMOJIS: [char; 4] = ['🎅', '🤶', '🎄', '🎁'];// Your Solution here ...fn emojify(s: &str) -> String { s.chars() .enumerate() .map(|(i, _)| CHRISTMAS_EMOJIS[i % CHRISTMAS_EMOJIS.len()]) .collect()}pub trait Anonymize { fn anonymize_email(&self) -> String;}impl Anonymize for String { fn anonymize_email(&self) -> String { let mut parts = self.split("@"); let tag = emojify(parts.next().unwrap_or("")); match parts.next() { Some(domain) => format!("{}@{}", tag, domain), None => tag } }}pub fn main() { let emails = vec![ "[email protected]".to_string(), "[email protected]".to_string(), "[email protected]".to_string(), "[email protected]".to_string(), "[email protected]".to_string(), "[email protected]".to_string(), "[email protected]".to_string(), "[email protected]".to_string(), "[email protected]".to_string(), "[email protected]".to_string(), ]; for email in emails { let anonymized_email = email.anonymize_email(); // This is the API that Santa wants! println!("Original: {} -> Anonymized: {}", email, anonymized_email); }}
// Ensure all relevant items are marked with `pub` keywordconst CHRISTMAS_EMOJIS: [char; 4] = ['🎅', '🤶', '🎄', '🎁'];// Your Solution here ...fn emojify(s: &str) -> String { s.chars() .enumerate() .map(|(i, _)| CHRISTMAS_EMOJIS[i % CHRISTMAS_EMOJIS.len()]) .collect()}pub trait Anonymize { fn anonymize_email(&self) -> String;}impl Anonymize for String { fn anonymize_email(&self) -> String { let mut parts = self.split("@"); let tag = emojify(parts.next().unwrap_or("")); match parts.next() { Some(domain) => format!("{}@{}", tag, domain), None => tag } }}pub fn main() { let emails = vec![ "[email protected]".to_string(), "[email protected]".to_string(), "[email protected]".to_string(), "[email protected]".to_string(), "[email protected]".to_string(), "[email protected]".to_string(), "[email protected]".to_string(), "[email protected]".to_string(), "[email protected]".to_string(), "[email protected]".to_string(), ]; for email in emails { let anonymized_email = email.anonymize_email(); // This is the API that Santa wants! println!("Original: {} -> Anonymized: {}", email, anonymized_email); }}
// Ensure all relevant items are marked with `pub` keywordconst CHRISTMAS_EMOJIS: [char; 4] = ['🎅', '🤶', '🎄', '🎁'];// Your Solution here ...pub trait StringExt { fn anonymize_email(&self) -> Self;}impl StringExt for String { fn anonymize_email(&self) -> Self { match self.find("@") { Some(index) => self[..index].to_christmas_emoji() + &self[index..], None => self.to_christmas_emoji(), } }}pub trait StrExt { fn to_christmas_emoji(&self) -> String;}impl StrExt for str { fn to_christmas_emoji(&self) -> String { let mut result = String::new(); for i in 0..self.chars().count() { let emoji_index = i % CHRISTMAS_EMOJIS.len(); result.push(CHRISTMAS_EMOJIS[emoji_index]); } result }}pub fn main() { let emails = vec![ "[email protected]".to_string(), "[email protected]".to_string(), "[email protected]".to_string(), "[email protected]".to_string(), "[email protected]".to_string(), "[email protected]".to_string(), "[email protected]".to_string(), "[email protected]".to_string(), "[email protected]".to_string(), "[email protected]".to_string(), ]; for email in emails { let anonymized_email = email.anonymize_email(); // This is the API that Santa wants! println!("Original: {} -> Anonymized: {}", email, anonymized_email); }}#[cfg(test)]mod test { use crate::StringExt; #[test] fn test_anonymize() { assert_eq!("santa".to_string().anonymize_email(), "🎅🤶🎄🎁🎅"); assert_eq!("[email protected]".to_string().anonymize_email(), "🎅🤶🎄🎁🎅@north.pole"); }}
// Ensure all relevant items are marked with `pub` keywordconst CHRISTMAS_EMOJIS: [char; 4] = ['🎅', '🤶', '🎄', '🎁'];// Your Solution here ...fn emojify(s: &str) -> String { s .chars() .enumerate() .map(|(i, _)| {CHRISTMAS_EMOJIS[i % CHRISTMAS_EMOJIS.len()]}) .collect()}pub trait Anonymize { fn anonymize_email(&self) -> String;}impl Anonymize for String { fn anonymize_email(&self) -> String { if let Some(at_index) = self.find('@') { format!("{}{}", emojify(&self[..at_index]), &self[at_index..]) } else { emojify(&self[..]) } }}pub fn main() { let emails = vec![ "[email protected]".to_string(), "[email protected]".to_string(), "[email protected]".to_string(), "[email protected]".to_string(), "[email protected]".to_string(), "[email protected]".to_string(), "[email protected]".to_string(), "[email protected]".to_string(), "[email protected]".to_string(), "[email protected]".to_string(), ]; for email in emails { let anonymized_email = email.anonymize_email(); // This is the API that Santa wants! println!("Original: {} -> Anonymized: {}", email, anonymized_email); }}
// Ensure all relevant items are marked with `pub` keywordconst CHRISTMAS_EMOJIS: [char; 4] = ['🎅', '🤶', '🎄', '🎁'];// Your Solution here ...pub fn emojify(s: &str) -> String { s.chars() .enumerate() .map(|(i, _)| CHRISTMAS_EMOJIS[i % CHRISTMAS_EMOJIS.len()]) .collect()}pub trait Anonymize { fn anonymize_email(&self) -> String;}impl Anonymize for String { fn anonymize_email(&self) -> String { if let Some(at_index) = self.find("@") { format!("{}{}", emojify(&self[..at_index]), &self[at_index..]) } else { emojify(self) } }}pub fn main() { let emails = vec![ "[email protected]".to_string(), "[email protected]".to_string(), "[email protected]".to_string(), "[email protected]".to_string(), "[email protected]".to_string(), "[email protected]".to_string(), "[email protected]".to_string(), "[email protected]".to_string(), "[email protected]".to_string(), "[email protected]".to_string(), ]; for email in emails { let anonymized_email = email.anonymize_email(); // This is the API that Santa wants! println!("Original: {} -> Anonymized: {}", email, anonymized_email); }}
// Ensure all relevant items are marked with `pub` keywordconst CHRISTMAS_EMOJIS: [char; 4] = ['🎅', '🤶', '🎄', '🎁'];// Your Solution here ...pub trait Anonymize { fn anonymize_email(&self) -> String;}pub fn emojify(s : &str) -> String { s.chars() .enumerate() .map(|(i, _)| CHRISTMAS_EMOJIS[i % CHRISTMAS_EMOJIS.len()]) .collect()} impl Anonymize for String { fn anonymize_email(&self) -> String { let parts: Vec<&str> = self.split('@').collect(); // If it looks like a valid email (has one @) if parts.len() == 2 { let local = parts[0]; let domain = parts[1]; let emoji_local: String = emojify(local); format!("{}@{}", emoji_local, domain) } else { // Invalid email — emoji-fy the whole thing emojify(&self[..]) } }}pub fn main() { let emails = vec![ "[email protected]".to_string(), "[email protected]".to_string(), "[email protected]".to_string(), "[email protected]".to_string(), "[email protected]".to_string(), "[email protected]".to_string(), "[email protected]".to_string(), "[email protected]".to_string(), "[email protected]".to_string(), "[email protected]".to_string(), ]; for email in emails { let anonymized_email = email.anonymize_email(); // This is the API that Santa wants! println!("Original: {} -> Anonymized: {}", email, anonymized_email); }}
// Ensure all relevant items are marked with `pub` keywordconst CHRISTMAS_EMOJIS: [char; 4] = ['🎅', '🤶', '🎄', '🎁'];// Your Solution here ...pub trait Email { fn anonymize_email(&self) -> String;}impl Email for String { fn anonymize_email(&self) -> String { println!("{}", self); let parts = self.split_once('@'); let (local, domain) = parts.unwrap_or((self,"")); let censored: String = local .chars() .enumerate() .map(|(i, _)| CHRISTMAS_EMOJIS[i as usize % 3]) .collect(); let result = format!("{}@{}", censored,domain); println!("censored: {result}"); result }}pub fn main() { let emails = vec![ "[email protected]".to_string(), "[email protected]".to_string(), "[email protected]".to_string(), "[email protected]".to_string(), "[email protected]".to_string(), "[email protected]".to_string(), "[email protected]".to_string(), "[email protected]".to_string(), "[email protected]".to_string(), "[email protected]".to_string(), ]; for email in emails { let anonymized_email = email.anonymize_email(); // This is the API that Santa wants! println!("Original: {} -> Anonymized: {}", email, anonymized_email); }}
// Ensure all relevant items are marked with `pub` keywordconst CHRISTMAS_EMOJIS: [char; 4] = ['🎅', '🤶', '🎄', '🎁'];pub trait AnonymizeEmail { fn anonymize_email(&self) -> String;}impl AnonymizeEmail for String { fn anonymize_email(&self) -> String { let parts = self.split_once("@"); let (local, domain) = parts.unwrap_or((self, "")); let nlocal: String = local .chars() .enumerate() .map(|(i, c)| CHRISTMAS_EMOJIS[(i + c as usize) & 3]) .collect(); if parts.is_none() { println!("Invalid email {}", self); nlocal } else { println!("email {} => {}@{}", self, nlocal, domain); format!("{}@{}", nlocal, domain) } }}pub fn main() { let emails = vec![ "[email protected]".to_string(), "[email protected]".to_string(), "[email protected]".to_string(), "[email protected]".to_string(), "[email protected]".to_string(), "[email protected]".to_string(), "[email protected]".to_string(), "[email protected]".to_string(), "[email protected]".to_string(), "[email protected]".to_string(), ]; for email in emails { let anonymized_email = email.anonymize_email(); // This is the API that Santa wants! println!("Original: {} -> Anonymized: {}", email, anonymized_email); }}
// Ensure all relevant items are marked with `pub` keywordconst CHRISTMAS_EMOJIS: [char; 4] = ['🎅', '🤶', '🎄', '🎁'];// Your Solution here ...pub trait Anonymizer { fn anonymize_email(&self) -> String;}impl Anonymizer for String { fn anonymize_email(&self) -> String { let list_of_str: Vec<&str> = self.split("@").collect(); // 0 element should be anon let anon = list_of_str[0].chars().enumerate().map(|(i, c)| CHRISTMAS_EMOJIS[((i + 1) % CHRISTMAS_EMOJIS.len())]) .collect::<String>(); if list_of_str.len() > 1 {// let final_message: String = anon + "@" + &list_of_str[1..].iter().map(|item| item.to_string()).reduce(|a: String, b: String| a + &b).unwrap_or("".to_string()).as_str(); return anon + "@" + list_of_str[1]; } return anon + "@"; }}pub fn main() { let emails = vec![ "[email protected]".to_string(), "[email protected]".to_string(), "[email protected]".to_string(), "[email protected]".to_string(), "[email protected]".to_string(), "[email protected]".to_string(), "[email protected]".to_string(), "[email protected]".to_string(), "[email protected]".to_string(), "[email protected]".to_string(), ]; for email in emails { let anonymized_email = email.anonymize_email(); // This is the API that Santa wants! println!("Original: {} -> Anonymized: {}", email, anonymized_email); }}
// Ensure all relevant items are marked with `pub` keywordconst CHRISTMAS_EMOJIS: [char; 4] = ['🎅', '🤶', '🎄', '🎁'];// Your Solution here ...pub trait Anonymized{ fn anonymize_email(&self)->Self;}impl Anonymized for String{ fn anonymize_email(&self)->String{ let mut ret = String::new(); let offset = self.find("@").unwrap_or(self.len()); for (i, c) in self.chars().enumerate(){ ret.push(if i < offset {CHRISTMAS_EMOJIS[i%4]} else {c}); } return ret; }}pub fn main() { let emails = vec![ "[email protected]".to_string(), "[email protected]".to_string(), "[email protected]".to_string(), "[email protected]".to_string(), "[email protected]".to_string(), "[email protected]".to_string(), "[email protected]".to_string(), "[email protected]".to_string(), "[email protected]".to_string(), "[email protected]".to_string(), ]; for email in emails { let anonymized_email = email.anonymize_email(); // This is the API that Santa wants! println!("Original: {} -> Anonymized: {}", email, anonymized_email); }}
// Ensure all relevant items are marked with `pub` keywordconst CHRISTMAS_EMOJIS: [char; 4] = ['🎅', '🤶', '🎄', '🎁'];// Your Solution here ...//pub struct Email {// pub email: String//}//impl email { no need for all of this, just implement the method directly. //////pub trait MyStringMethods { fn anonymize_email(&self) -> String;}impl MyStringMethods for String { fn anonymize_email(&self) -> String { if !self.contains("@") { let length = self.len(); let mut n = 0; let mut anon = String::new(); while n < length { //you can just + 1 as in length +1 if you need to if n > 3 { anon.push(CHRISTMAS_EMOJIS[n % 3]); } else { anon.push(CHRISTMAS_EMOJIS[n]); } n=n+1; } anon } else { let parts: Vec<&str> = self.split("@").collect(); let name_length = parts[0].len(); let mut n = 0; let mut anon = String::new(); while n < name_length { if n > 3 { anon.push(CHRISTMAS_EMOJIS[n % 3]); } else { anon.push(CHRISTMAS_EMOJIS[n]); } n=n+1; } println!("{anon}"); let domain = parts[1]; let at = "@"; let output = anon + at + domain; output } }}pub fn main() { let emails = vec![ "[email protected]".to_string(), "[email protected]".to_string(), "[email protected]".to_string(), "[email protected]".to_string(), "[email protected]".to_string(), "[email protected]".to_string(), "[email protected]".to_string(), "[email protected]".to_string(), "[email protected]".to_string(), "[email protected]".to_string(), "[email protected]".to_string(), ]; for email in emails { let anonymized_email = email.anonymize_email(); // This is the API that Santa wants! println!("Original: {} -> Anonymized: {}", email, anonymized_email); }}
// Ensure all relevant items are marked with `pub` keywordconst CHRISTMAS_EMOJIS: [char; 4] = ['🎅', '🤶', '🎄', '🎁'];// Your Solution here ...pub trait Anonymize{ fn anonymize_email(&self) ->String;}impl Anonymize for String{ fn anonymize_email(&self) ->String{ let mut anonymize_email_vec: Vec<char> = self.chars().collect(); for(i, char) in self.chars().enumerate(){ if char == '@'{ break; } anonymize_email_vec[i] = CHRISTMAS_EMOJIS[i % CHRISTMAS_EMOJIS.len()]; } anonymize_email_vec.into_iter().collect::<String>() }}pub fn main() { let emails = vec![ "[email protected]".to_string(), "[email protected]".to_string(), "[email protected]".to_string(), "[email protected]".to_string(), "[email protected]".to_string(), "[email protected]".to_string(), "[email protected]".to_string(), "[email protected]".to_string(), "[email protected]".to_string(), "[email protected]".to_string(), ]; for email in emails { let anonymized_email = email.anonymize_email(); // This is the API that Santa wants! println!("Original: {} -> Anonymized: {}", email, anonymized_email); }}
// Ensure all relevant items are marked with `pub` keyworduse std::char;const CHRISTMAS_EMOJIS: [char; 4] = ['🎅', '🤶', '🎄', '🎁'];pub trait Anonymize { fn anonymize_email(&self) -> String;}impl Anonymize for String { fn anonymize_email(&self) -> String { let mut anonymous_char_vec: Vec<char> = self.chars().collect(); for (i, char) in self.chars().enumerate() { if char == '@' { break; } anonymous_char_vec[i] = CHRISTMAS_EMOJIS[i % CHRISTMAS_EMOJIS.len()]; } anonymous_char_vec.into_iter().collect::<String>() }}pub fn main() { let emails = vec![ "[email protected]".to_string(), "[email protected]".to_string(), "[email protected]".to_string(), "[email protected]".to_string(), "[email protected]".to_string(), "[email protected]".to_string(), "[email protected]".to_string(), "[email protected]".to_string(), "[email protected]".to_string(), "[email protected]".to_string(), ]; for email in emails { let anonymized_email = email.anonymize_email(); // This is the API that Santa wants! println!("Original: {} -> Anonymized: {}", email, anonymized_email); }}
use std::vec::Vec;// Ensure all relevant items are marked with `pub` keywordconst CHRISTMAS_EMOJIS: [char; 4] = ['🎅', '🤶', '🎄', '🎁'];// Your Solution here ...pub trait Anonymizable { fn anonymize_email(&self) -> String;}impl Anonymizable for String { fn anonymize_email(&self) -> String { // Do this naively. Anonymize up until @ character. // We're not testing actual email regex let mut chars = Vec::new(); let mut found_address = false; for (i, c) in self.char_indices() { if c == '@' { found_address = true; } if c != '@' && !found_address { chars.push(CHRISTMAS_EMOJIS[i % 4]); } else { chars.push(c); } } String::from_iter(chars) }}pub fn main() { let emails = vec![ "[email protected]".to_string(), "[email protected]".to_string(), "[email protected]".to_string(), "[email protected]".to_string(), "[email protected]".to_string(), "[email protected]".to_string(), "[email protected]".to_string(), "[email protected]".to_string(), "[email protected]".to_string(), "[email protected]".to_string(), ]; for email in emails { let anonymized_email = email.anonymize_email(); // This is the API that Santa wants! println!("Original: {} -> Anonymized: {}", email, anonymized_email); }}
// Ensure all relevant items are marked with `pub` keywordconst CHRISTMAS_EMOJIS: [char; 4] = ['🎅', '🤶', '🎄', '🎁'];// Your Solution here ...pub trait Anonymize { fn anonymize_email(&self) -> String;}impl Anonymize for String { fn anonymize_email(&self) -> String { let parts: Vec<&str> = self.split('@').collect(); let str_length = parts[0].len(); let mut new_string = String::from(""); match parts.len() { 2 => for i in 0..str_length { new_string.push(CHRISTMAS_EMOJIS[i%4]); }, _ => for i in 0..self.len() { new_string.push(CHRISTMAS_EMOJIS[i%4]); } } new_string }}pub fn main() { let emails = vec![ "[email protected]".to_string(), "[email protected]".to_string(), "[email protected]".to_string(), "[email protected]".to_string(), "[email protected]".to_string(), "[email protected]".to_string(), "[email protected]".to_string(), "[email protected]".to_string(), "[email protected]".to_string(), "[email protected]".to_string(), ]; for email in emails { let anonymized_email = email.anonymize_email(); // This is the API that Santa wants! println!("Original: {} -> Anonymized: {}", email, anonymized_email); }}
// Ensure all relevant items are marked with `pub` keywordconst CHRISTMAS_EMOJIS: [char; 4] = ['🎅', '🤶', '🎄', '🎁'];// Your Solution here ...pub trait Anonymize { fn anonymize_email(&self) -> String;}impl Anonymize for String { fn anonymize_email(&self) -> String { let parts: Vec<&str> = self.split('@').collect(); let str_length = parts[0].len(); let mut new_string = String::from(""); match parts.len() { 2 => for i in 0..str_length { new_string.push(CHRISTMAS_EMOJIS[i%4]); }, _ => for i in 0..self.len() { new_string.push(CHRISTMAS_EMOJIS[i%4]); } } new_string }}pub fn main() { let emails = vec![ "[email protected]".to_string(), "[email protected]".to_string(), "[email protected]".to_string(), "[email protected]".to_string(), "[email protected]".to_string(), "[email protected]".to_string(), "[email protected]".to_string(), "[email protected]".to_string(), "[email protected]".to_string(), "[email protected]".to_string(), ]; for email in emails { let anonymized_email = email.anonymize_email(); // This is the API that Santa wants! println!("Original: {} -> Anonymized: {}", email, anonymized_email); }}
// Ensure all relevant items are marked with `pub` keywordconst CHRISTMAS_EMOJIS: [char; 4] = ['🎅', '🤶', '🎄', '🎁'];// Your Solution here ...pub trait Anonymize { fn anonymize_email(&self) -> String;}impl Anonymize for String { fn anonymize_email(&self) -> String { let parts: Vec<&str> = self.split('@').collect(); let str_length = parts[0].len(); let mut new_string = String::from(""); match parts.len() { 2 => for i in 0..str_length { new_string.push(CHRISTMAS_EMOJIS[i%4]); }, _ => for i in 0..self.len() { new_string.push(CHRISTMAS_EMOJIS[i%4]); } } new_string }}pub fn main() { let emails = vec![ "[email protected]".to_string(), "[email protected]".to_string(), "[email protected]".to_string(), "[email protected]".to_string(), "[email protected]".to_string(), "[email protected]".to_string(), "[email protected]".to_string(), "[email protected]".to_string(), "[email protected]".to_string(), "[email protected]".to_string(), ]; for email in emails { let anonymized_email = email.anonymize_email(); // This is the API that Santa wants! println!("Original: {} -> Anonymized: {}", email, anonymized_email); }}
// Ensure all relevant items are marked with `pub` keywordconst CHRISTMAS_EMOJIS: [char; 4] = ['🎅', '🤶', '🎄', '🎁'];// Your Solution here ...pub trait Anonymize { fn anonymize_email(&self) -> String;}impl Anonymize for String { fn anonymize_email(&self) -> String { let parts: Vec<&str> = self.split('@').collect(); let str_length = parts[0].len(); let mut new_string = String::from(""); match parts.len() { 2 => for i in (0..str_length) { new_string.push(CHRISTMAS_EMOJIS[i%4]); }, _ => for i in (0..self.len()) { new_string.push(CHRISTMAS_EMOJIS[i%4]); } } new_string }}pub fn main() { let emails = vec![ "[email protected]".to_string(), "[email protected]".to_string(), "[email protected]".to_string(), "[email protected]".to_string(), "[email protected]".to_string(), "[email protected]".to_string(), "[email protected]".to_string(), "[email protected]".to_string(), "[email protected]".to_string(), "[email protected]".to_string(), ]; for email in emails { let anonymized_email = email.anonymize_email(); // This is the API that Santa wants! println!("Original: {} -> Anonymized: {}", email, anonymized_email); }}
// Ensure all relevant items are marked with `pub` keywordconst CHRISTMAS_EMOJIS: [char; 4] = ['🎅', '🤶', '🎄', '🎁'];// Your Solution here ...pub trait Anonymize { fn anonymize_email(&self) -> String;}impl Anonymize for String { fn anonymize_email(&self) -> String { let mut out : String = String::from(""); let mut chas_iter = self.chars().enumerate(); let mut flag = true; while let Some((index, ch)) = chas_iter.next() { if ch=='@' {flag = false;} match flag { true => {out.push(CHRISTMAS_EMOJIS[index%4]);}, false => {out.push(ch);}, } } out }}pub fn main() { let emails = vec![ "[email protected]".to_string(), "[email protected]".to_string(), "[email protected]".to_string(), "[email protected]".to_string(), "[email protected]".to_string(), "[email protected]".to_string(), "[email protected]".to_string(), "[email protected]".to_string(), "[email protected]".to_string(), "[email protected]".to_string(), ]; for email in emails { let anonymized_email = email.anonymize_email(); // This is the API that Santa wants! println!("Original: {} -> Anonymized: {}", email, anonymized_email); }}