Adding Files example to rustlings

This commit is contained in:
Calvin Brown 2020-09-21 14:47:16 -05:00
parent 4e9cd43b92
commit 8d9fe0580a
No known key found for this signature in database
GPG key ID: 023E6AD62A996C32
3 changed files with 36 additions and 1 deletions

View file

@ -0,0 +1,8 @@
### Files
The rust standard library supports reading files and writing them directly from the language!
`std::fs::File` is just `A reference to an open file on the filesystem.`
#### Book Sections
- [Reading a file](https://doc.rust-lang.org/stable/book/ch12-02-reading-a-file.html)

21
exercises/files/files1.rs Normal file
View file

@ -0,0 +1,21 @@
// files1.rs
// Address all the TODOs to make the tests pass!
// I AM NOT DONE
use std::fs::File;
use std::io::prelude::*;
pub fn create_file() -> std::io::Result<()> {
let mut file = File::create("foo.txt")?;
file.write_all(b"Hello, world!")?;
Ok(())
}
#[test]
fn read_file() {
create_file().unwrap();
//TODO: Read the file created above!
}

View file

@ -661,7 +661,7 @@ Very similar to the lines above and below. You've got this!
Step 3:
An iterator goes through all elements in a collection, but what if we've run out of
elements? What should we expect here? If you're stuck, take a look at
elements? What should we expect here? If you're stuck, take a look at
https://doc.rust-lang.org/std/iter/trait.Iterator.html for some ideas.
"""
@ -843,3 +843,9 @@ hint = """
The implementation of FromStr should return an Ok with a Person object,
or an Err with a string if the string is not valid.
This is almost like the `try_from_into` exercise."""
[[exercises]]
name = "files"
path = "exercises/files/files1.rs"
mode = "test"
hint = """Take a look at https://doc.rust-lang.org/std/fs/struct.File.html if you are stuck!"""