-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
0dce9bf
commit 9f6d5e9
Showing
2 changed files
with
37 additions
and
0 deletions.
There are no files selected for viewing
36 changes: 36 additions & 0 deletions
36
apps/blog/content/articles/implementing-the-write-trait.org
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
#+title: Implementing the Write trait | ||
#+categories: programming | ||
#+tags[]: rust | ||
#+date: [2024-08-18 Sun] | ||
|
||
|
||
Well, I did the Read trait so I gotta do the Write trait right? | ||
|
||
#+begin_src rust | ||
use std::io::Write; | ||
|
||
struct Foobar { | ||
data: String, | ||
} | ||
|
||
impl std::io::Write for Foobar { | ||
fn write(&mut self, buf: &[u8]) -> std::io::Result<usize> { | ||
let s = std::str::from_utf8(buf).expect("whops"); | ||
self.data.push_str(s); | ||
Ok(s.len()) | ||
} | ||
|
||
fn flush(&mut self) -> std::io::Result<()> { | ||
todo!() | ||
} | ||
} | ||
|
||
fn main() { | ||
let mut foobar = Foobar { | ||
data: String::from("helloworld"), | ||
}; | ||
|
||
let _ = foobar.write(".".as_bytes()); | ||
println!("{}", foobar.data) | ||
} | ||
#+end_src |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters