Skip to content

Commit

Permalink
Update README.md
Browse files Browse the repository at this point in the history
  • Loading branch information
SupperZum authored Dec 18, 2023
1 parent 00a3e28 commit eee701b
Showing 1 changed file with 35 additions and 3 deletions.
38 changes: 35 additions & 3 deletions 2_idioms/2_4_generic_in_type_out/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,23 @@ pub fn add_hi(v: &mut String) {
v.push_str(" Hi")
}

#[derive(AsMut, AsRef)]

pub struct Nickname(String);

// Implement AsRef for Nickname
impl AsRef<str> for Nickname {
fn as_ref(&self) -> &str {
&self.0
}
}

// Implement AsMut for Nickname
impl AsMut<String> for Nickname {
fn as_mut(&mut self) -> &mut String {
&mut self.0
}
}

impl Nickname {
// We want to own `nickname` inside `Nickname` value.
pub fn new(nickname: String) -> Self {
Expand All @@ -45,15 +60,32 @@ pub fn just_print_stringy<S: AsRef<str>>(v: S) {
println!("{}", v.as_ref())
}

pub fn add_hi<S: AsMut<String>>(v: S) {
pub fn add_hi<S: AsMut<String>>(mut v: S) {
v.as_mut().push_str(" Hi")
}

impl Nickname {
pub struct Nickname(String);

// Implement AsRef for Nickname
impl AsRef<str> for Nickname {
fn as_ref(&self) -> &str {
&self.0
}
}

// Implement AsMut for Nickname
impl AsMut<String> for Nickname {
fn as_mut(&mut self) -> &mut String {
&mut self.0
}
}

impl Nickname {
pub fn new<S: Into<String>>(nickname: S) -> Self {
Self(nickname.into())
}
}

```
And now our API is pleasant to use:
```rust
Expand Down

0 comments on commit eee701b

Please sign in to comment.