mirror of
https://github.com/rust-lang-nursery/rust-cookbook
synced 2024-11-24 20:43:07 +00:00
Update examples with "for_each"
Remove intermediate variable
This commit is contained in:
parent
ac20edd909
commit
02ed855b21
4 changed files with 27 additions and 42 deletions
23
src/app.md
23
src/app.md
|
@ -234,7 +234,9 @@ fn run() -> Result<()> {
|
|||
let file = File::open("archive.tar.gz")?;
|
||||
let mut archive = Archive::new(GzDecoder::new(file)?);
|
||||
let prefix = "bundle/logs";
|
||||
let entries = archive
|
||||
|
||||
println!("Extracted the following files:");
|
||||
archive
|
||||
.entries()?
|
||||
.filter_map(|e| e.ok())
|
||||
.map(|mut entry| -> Result<PathBuf> {
|
||||
|
@ -242,12 +244,10 @@ fn run() -> Result<()> {
|
|||
let path = entry.path()?.strip_prefix(prefix)?.to_owned();
|
||||
entry.unpack(&path)?;
|
||||
Ok(path)
|
||||
});
|
||||
})
|
||||
.filter_map(|e| e.ok())
|
||||
.for_each(|x| println!("> {}", x.display()));
|
||||
|
||||
println!("Extracted the following files:");
|
||||
for path in entries.filter_map(|e| e.ok()) {
|
||||
println!("> {}", path.display());
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
#
|
||||
|
@ -467,12 +467,11 @@ fn is_not_hidden(entry: &DirEntry) -> bool {
|
|||
}
|
||||
|
||||
fn main() {
|
||||
for result in WalkDir::new(".")
|
||||
.into_iter()
|
||||
.filter_entry(|e| is_not_hidden(e))
|
||||
.filter_map(|v| v.ok()) {
|
||||
println!("{}", result.path().display());
|
||||
}
|
||||
WalkDir::new(".")
|
||||
.into_iter()
|
||||
.filter_entry(|e| is_not_hidden(e))
|
||||
.filter_map(|v| v.ok())
|
||||
.for_each(|x| println!("{}", x.path().display()));
|
||||
}
|
||||
```
|
||||
|
||||
|
|
|
@ -309,8 +309,7 @@ fn run() -> Result<()> {
|
|||
([0-9a-fA-F]+) # commit hash
|
||||
(.*) # The commit message")?;
|
||||
|
||||
let stdout = String::from_utf8(output.stdout)?;
|
||||
let commits = stdout
|
||||
String::from_utf8(output.stdout)?
|
||||
.lines()
|
||||
.filter_map(|line| pattern.captures(line))
|
||||
.map(|cap| {
|
||||
|
@ -319,11 +318,8 @@ fn run() -> Result<()> {
|
|||
message: cap[2].trim().to_string(),
|
||||
}
|
||||
})
|
||||
.take(5);
|
||||
|
||||
for commit in commits {
|
||||
println!("{:?}", commit);
|
||||
}
|
||||
.take(5)
|
||||
.for_each(|x| println!("{:?}", x));
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
@ -491,14 +487,11 @@ fn run() -> Result<()> {
|
|||
]).case_insensitive(true)
|
||||
.build()?;
|
||||
|
||||
let filtered = buffered
|
||||
buffered
|
||||
.lines()
|
||||
.filter_map(|line| line.ok())
|
||||
.filter(|line| set.is_match(line.as_str()));
|
||||
|
||||
for line in filtered {
|
||||
println!("{}", line);
|
||||
}
|
||||
.filter(|line| set.is_match(line.as_str()))
|
||||
.for_each(|x| println!("{}", x));
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
@ -586,9 +579,7 @@ fn run() -> Result<()> {
|
|||
// acquire access
|
||||
let db = FRUIT.lock().map_err(|_| "Failed to acquire MutexGuard")?;
|
||||
|
||||
for (i, item) in db.iter().enumerate() {
|
||||
println!("{}: {}", i, item);
|
||||
}
|
||||
db.iter().enumerate().for_each(|(i, item)| println!("{}: {}", i, item));
|
||||
// release access
|
||||
}
|
||||
insert("grape")?;
|
||||
|
|
|
@ -131,9 +131,7 @@ fn run() -> Result<()> {
|
|||
.filter_map(|x| x.err())
|
||||
.collect();
|
||||
|
||||
for failure in &image_failures {
|
||||
println!("{}", failure.display_chain());
|
||||
}
|
||||
image_failures.iter().for_each(|x| println!("{}", x.display_chain()));
|
||||
|
||||
println!("{} thumbnails saved successfully", files.len() - image_failures.len());
|
||||
Ok(())
|
||||
|
|
19
src/net.md
19
src/net.md
|
@ -860,14 +860,10 @@ use select::predicate::Name;
|
|||
fn run() -> Result<()> {
|
||||
let res = reqwest::get("https://www.rust-lang.org/en-US/")?;
|
||||
|
||||
let document = Document::from_read(res)?;
|
||||
|
||||
let links = document.find(Name("a"))
|
||||
.filter_map(|n| n.attr("href"));
|
||||
|
||||
for link in links {
|
||||
println!("{}", link);
|
||||
}
|
||||
Document::from_read(res)?
|
||||
.find(Name("a"))
|
||||
.filter_map(|n| n.attr("href"))
|
||||
.for_each(|x| println!("{}", x));
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
@ -940,9 +936,10 @@ fn run() -> Result<()> {
|
|||
.filter_map(|link| base_parser.parse(link).ok())
|
||||
.collect();
|
||||
|
||||
for link in links.iter().filter(|link| check_link(link).ok() == Some(false)) {
|
||||
println!("{} is broken.", link);
|
||||
}
|
||||
links
|
||||
.iter()
|
||||
.filter(|link| check_link(link).ok() == Some(false))
|
||||
.for_each(|x| println!("{} is broken.", x));
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue