Hi, I'm also new to Rust and used to the |>
operator in Elm. In my mind, the main difference between piping and methods, is that methods only work if the creator of the function thought of the use case before and made it a method. Let's take serde_json::from_reader
as a concrete example.
To read a json file, I'm currently doing:
let json_file_path = Path::new("path/to/file.json");
let json_file = File::open(json_file_path).expect("file not found");
let deserialized_data: SomeDataType =
serde_json::from_reader(json_file).expect("error while reading json");
While with an "elmish" piping, I would do something in the like of:
let deserialized_data: SomeDataType =
Path::new("path/to/file.json")
|> File::open()
|> expect("file not found")
|> serde_json::from_reader()
|> expect("error while reading json")
I get that it can be a matter of taste, but I hate naming temporary things just to avoid having nested parenthesis, that's why I like the piping syntax.
Any better way than my current rust version is welcomed! I'm still new to rust so struggling a bit with the way of writing things.