Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions 1_concepts/1_5_convert_cast_deref/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,7 @@ name = "step_1_5"
version = "0.1.0"
edition = "2024"
publish = false

[dependencies]
regex = "1.12.2"
rand = "0.9.2"
118 changes: 117 additions & 1 deletion 1_concepts/1_5_convert_cast_deref/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,119 @@
use rand::Rng;
use regex::Regex;
use std::borrow::Borrow;
use std::collections::HashMap;
use std::hash::{Hash, Hasher};
use std::{ops::Deref, sync::LazyLock};

fn main() {
println!("Implement me!");
email_check();
random_check();
}

static EMAIL_REGEX: LazyLock<Regex> = LazyLock::new(|| {
Regex::new(r"^[a-zA-Z0-9.!#$%&'*+/=?^_`{|}~-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*$")
.expect("Invalid regex pattern")
});

#[derive(Debug, Clone)]
struct EmailString(String);
impl EmailString {
fn new(email: &str) -> Result<EmailString, &'static str> {
if EMAIL_REGEX.is_match(email) {
return Ok(EmailString(email.into()));
}

Err("Invalid email format".into())
}
}
impl Deref for EmailString {
type Target = str;

fn deref(&self) -> &Self::Target {
&self.0
}
}
impl TryFrom<&str> for EmailString {
type Error = &'static str;

fn try_from(email: &str) -> Result<Self, Self::Error> {
Self::new(email)
}
}
impl Hash for EmailString {
fn hash<H: Hasher>(&self, state: &mut H) {
self.0.hash(state);
}
}
impl PartialEq for EmailString {
fn eq(&self, other: &Self) -> bool {
self.0 == other.0
}
}
impl Eq for EmailString {}

impl Borrow<str> for EmailString {
fn borrow(&self) -> &str {
&self.0 //
}
}

fn email_check() {
let valid_email_str: &'static str = "x@x.x";
let invalid_email_str: &'static str = "x";

// validation
assert!(EmailString::new(invalid_email_str.into()).is_err());
let valid_email = EmailString::new(&valid_email_str).unwrap();

// deref
assert_eq!(*valid_email, *valid_email_str);

// as_ref (auto generated by deref)
let _: &str = valid_email.as_ref();

// try from
assert!(EmailString::try_from(valid_email_str).is_ok());

// try into (auto generated using try from impl)
let email: Result<EmailString, _> = valid_email_str.try_into();
assert!(email.is_ok());

// Borrow<str>
let user_data: HashMap<EmailString, String> = HashMap::new();
user_data.get(valid_email_str);
}

pub struct Random<T> {
values: [T; 3],
}
impl<T> Random<T> {
pub fn new(v1: T, v2: T, v3: T) -> Self {
Self {
values: [v1, v2, v3],
}
}

// Better to use explicit method instead of deref based on
// https://doc.rust-lang.org/std/ops/trait.Deref.html#when-to-implement-deref-or-derefmut
// because users may be surprised if deref return random T
fn use_random(&self) -> &T {
let idx = rand::rng().random_range(0..=2);
&self.values[idx]
}
}
impl<T> Deref for Random<T> {
type Target = [T; 3];

fn deref(&self) -> &Self::Target {
&self.values
}
}

fn random_check() {
let r = Random::new(1, 2, 3);

for _ in 0..10 {
println!("{}", r.use_random());
}
}
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ Each step must be performed as a separate [PR (pull request)][PR] with an approp
- [ ] [1.2. Boxing and pinning][Step 1.2] (1 day)
- [ ] [1.3. Shared ownership and interior mutability][Step 1.3] (1 day)
- [ ] [1.4. Clone-on-write][Step 1.4] (1 day)
- [ ] [1.5. Conversions, casting and dereferencing][Step 1.5] (1 day)
- [x] [1.5. Conversions, casting and dereferencing][Step 1.5] (1 day)
- [ ] [1.6. Static and dynamic dispatch][Step 1.6] (1 day)
- [ ] [1.7. `Sized` and `?Sized` types][Step 1.7] (1 day)
- [ ] [1.8. Thread safety][Step 1.8] (1 day)
Expand Down