https://rust-lang-nursery.github.io/cli-wg/ – tracking repo: https://github.com/rust-lang-nursery/cli-wg/issues
Dylan-DPC on master
Fix README link (#162) (compare)
spacekookie on master
Clarify source position Make i… (compare)
error: The following required arguments were not provided:
<query|--self-update>
USAGE:
agrind [FLAGS] [OPTIONS] <query|--self-update>
use failure::ResultExt;
use exitfailure::ExitFailure;
fn main() -> Result<(), ExitFailure> {
let path = "test.txt";
let content = std::fs::read_to_string(path)
.with_context(|_| format!("could not read file `{}`", path))?;
println!("file content: {}", content);
Ok(())
}
do_hard_work()
(Showing a progress bar). When this book is more by example than this is a problem. Because I (newbee) can't try this code. Tried alredy to implement some downloader of files but got problems with open_ssl being included by many crates.Trying to follow the cli-wg tutorial, this is my implementation: https://github.com/crodjer/grrs/blob/master/src/main.rs#L23
I am using BufReader to avoid copying lines:
let lines = BufReader::new(f)
.lines()
.filter(Result::is_ok)
.map(Result::unwrap);
I then feed this iterator to my library function to find_matches:
pub fn find_matches<I>(lines: I, pattern: &str) -> Vec<String>
where
I: Iterator<Item = String>,
{
lines.filter(|line| line.contains(&pattern)).collect()
}
But, I want to avoid return a vector and want to return an iterator which main
can then lazily render. After removing collect
, I am unable to figure out the type signatures.
find_matches
so that I only deal with lazy iterators?
Okay, so this works:
pub fn find_matches<'a, I: 'a>(lines: I, pattern: &'a str) -> impl Iterator<Item = String> + 'a
where
I: Iterator<Item = String>,
{
lines.filter(move |line| line.contains(pattern))
}
Based on: https://stackoverflow.com/a/27649089. Don't know why though.
@crodjer that's a very good solution! tbh, i had never fully implemented the tool with all features and lazy streaming… :sweat_smile: thanks for doing that!
you can read more on the impl Trait
feature in RFC #1522 and an expansion of it in RFC #1951
you also can write find_matches
like this if you want the param and return type to be more symmetrical:
pub fn find_matches<'a>(
lines: impl Iterator<Item = String> + 'a,
pattern: &'a str,
) -> impl Iterator<Item = String> + 'a {
lines.filter(move |line| line.contains(pattern))
}
.filter(Result::is_ok).map(Result::unwrap)
you can also .filter_map(Result::ok)
by the way
let status = Arc::new(Mutex::new(0));
loop {
if *status.lock().unwrap() >= 2 {
break;
}
select! {
recv(ticks) -> _ => {
println!("working!");
}
recv(ctrl_c_events) -> _ => {
let status = status.clone();
*status.lock().unwrap() += 1;
thread::spawn(move || {
println!("Shutting down...");
thread::sleep(Duration::from_secs(2));
println!("Goodbye!");
*status.lock().unwrap() += 1;
});
}
}
}
.description
method instead of the .help
method in the code.