3 ms·
I don't know Rust but converting that code snippet of Go code you wrote to (non-idiomatic [wouldn't use upper-case characters, namespaces used doesn't actually
by capableweb 5y ago
I don't know Rust but converting that code snippet of Go code you wrote to (non-idiomatic [wouldn't use upper-case characters, namespaces used doesn't actually exists]) Clojure:
(defn Read [path]
(io/ReadToEnd (os/Open path)))
Now that's what I call readable!
- brundolf 5y agoClojure is also a much higher-level language than Rust (and arguably Go too)
- scottlamb 5y agoYou can chain calls in Rust. If read_to_end were written to allocate and return a fresh Vec, you could do this: File::open(path)?.read_to_end() but instead read_to_end is written to take a presupplied Vec so the caller has the option to reduce allocations.
- masklinn 5y ago> Now that's what I call readable! Unsurprisingly APIs can be more readable when you don't ponder why they're designed the way they are and completely ignore their purpose. The `read_to_end` API[0] was designed such that the buffer could be reused in order to avoid unnecessary allocations. There are helpers for common tasks, though people may not know or notice them. In the case of `Read::read_to_end`, it's called std::fs::read[1], which actually preallocates the vector based on file size internally so might be faster than hand-rolling `read_to_end`. So here's your code in Rust, for the last 3 years or so (since 1.26 was released): fn read<P: AsRef<Path>>(path: P) -> io::Result<Vec<u8>> { std::fs::read(path) } [0] and all of the Read trait in general [1] https://doc.rust-lang.org/std/fs/fn.read.html https://doc.rust-lang.org/std/fs/fn.read.html