Adding Files example to rustlings
This commit is contained in:
parent
4e9cd43b92
commit
8d9fe0580a
8
exercises/files/README.md
Normal file
8
exercises/files/README.md
Normal 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
21
exercises/files/files1.rs
Normal 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!
|
||||
}
|
|
@ -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!"""
|
Loading…
Reference in a new issue