How to do something similar to this D and Java code in Rust?
Java:
import java.nio.file.*;
import java.io.*;
public class Main {
public static void main( String[] args ) throws IOException
{
Files.lines(Paths.get("/home/kozak/test.txt"))
.filter(s -> s.endsWith("/bin/bash"))
.map(s -> s.split(":", 2)[0])
.forEach(System.out::println);
}
}
D language:
import std.algorithm;
import std.stdio;
void main() {
File("/home/kozak/test.txt")
.byLine
.filter!((s)=>s.endsWith("/bin/bash"))
.map!((s)=>s.splitter(":").front)
.each!writeln;
}
I try it, but I am lost with all this ownership stuff
my rust code:
use std::io::BufReader;
use std::fs::File;
use std::io::BufRead;
use std::io::Lines;
fn main() {
let file = match File::open("/etc/passwd") {
Ok(file) => file,
Err(..) => panic!("room"),
};
let mut reader = BufReader::new(&file);
for line in reader.lines().filter_map(
|x| if match x { Ok(v) => v.rmatches("/bin/bash").count() > 0, Err(e) => false}
{ match x { Ok(v2) => Some(v2.split(":").next()), Err(e2) => None }} else
{ None })
{
print!("{}", line.unwrap() );
}
}