Crates.io | slurp |
lib.rs | slurp |
version | 1.0.1 |
source | src |
created_at | 2017-10-06 16:36:14.27229 |
updated_at | 2017-10-06 16:47:47.083629 |
description | Small wrapper methods for loading files |
homepage | |
repository | https://github.com/QuietMisdreavus/slurp |
max_upload_size | |
id | 34591 |
size | 9,546 |
some little helper functions to load files without the boilerplate
slurp
exists to solve a very particular problem. If you're trying to load a file from disk,
there's some boilerplate code that you need to write up to handle the Read
trait effectively:
// gotta declare the output before??
let mut buf = String::new();
// gotta declare the file reader separately??
let mut file = File::open(filename).unwrap();
file.read_to_string(&mut buf).unwrap();
Now, there's a really good reason Read
doesn't allocate its own Vecs or Strings: This way, you can
really easily reuse your own buffers. But what if you want to hang onto each file's contents
separately? Or what if you're only loading one file ever? All this extra ability just gets in the
way when you just want to slurp
a file off the disk.
That's where this crate comes in! By adding this crate (or copying its functions into your project - they're really quite small), all that boilerplate gets wrapped away into its own function!
let my_file = slurp::read_all_to_string(filename).unwrap();
slurp
also contains other functions to load into a byte vector (read_all_bytes
) or into a Vec
with one String for each line (read_all_lines
).
To use this crate in your project, add the following to your Cargo.toml:
[dependencies]
slurp = "1.0.1"
...and the following in your crate root:
extern crate slurp;
slurp
is licensed under the Unlicense, which is effectively a public domain dedication. If you
don't want to add an entirely separate crate for this, feel free to copy the code into your own
project!