pub fn open(fname: &str) -> Result<BufReader<File>>Expand description
Opens a file for reading.
With this, you can read a file line by line.
It is similar to Python’s f = open("text.txt").
§Examples
use std::io::BufRead;
let f = jabba_lib::jfs::open("Cargo.toml").unwrap();
for line in f.lines() {
let line = line.unwrap();
println!("{}", line);
}In Python, it would look like this:
f = open("Cargo.toml")
for line in f:
line = line.rstrip("\n")
print(line)Examples found in repository?
More examples
examples/fs.rs (line 11)
4fn main() {
5 let lines = jfs::readlines("Cargo.toml").unwrap();
6 println!("Number of lines in Cargo.toml: {}", lines.len());
7 println!("---");
8 let content = jfs::read("Cargo.toml").unwrap();
9 println!("Number of characters in Cargo.toml: {}", content.len());
10 println!("---");
11 let f = jfs::open("Cargo.toml").unwrap();
12 for line in f.lines() {
13 let line = line.unwrap();
14 println!("{}", line);
15 }
16}