4 ms·
Some nitpicks, since you're almost asking for them: 'read' taking 'ref P path' isn't entirely accurate. In Rust, 'path' is moved into the 'read' function, whic
by LegionMammal978 4y ago
Some nitpicks, since you're almost asking for them:
'read' taking 'ref P path' isn't entirely accurate. In Rust, 'path' is moved into the 'read' function, which destroys it after calling 'inner'. (If the caller doesn't want it to be destroyed, it provides a reference, i.e., a safe non-null immutable pointer; when 'read' destroys the reference, nothing happens.) As I understand it, 'ref P path' in D still gives the caller the ability to access 'path' after calling 'read' and the responsibility for destroying it.
Also, the '?'s are important for error handling: the 'File::open()?' line basically expands to
let mut file;
match File::open(path) {
Ok(value) => { file = value; }
Err(error) => { return Err(error); }
}
and similar for the 'read_to_end()?', except the result is discarded. And one of the reasons (apart from flexibility) that 'read_to_end()' takes an external vector as input is so that the caller can retain the result of a partial read; otherwise, the function would have to return a vector on error as well as on success.
- WalterBright 4y agoI understand that Rust has somewhat different semantics, but for purposes of illustration I focused on the syntax. For example, D could would likely throw an exception, rather than use pattern matching, for error handling.