The `~[u8]` would be written `std::unique_ptr<u8>` in C++. They're used everywhere in Rust, and having to write out unique_ptr would get awfully tiresome.
The `acc` on its own line is a result of "the last statement gets returned" like many functional languages do. If you were writing C++, you'd write `return acc;` instead. This one is more of a convenience, and you could get away without it.
Wouldn't it be `std::unique_ptr<std::vector<u8>>`?
(1) Still being worked on.
fn hexdigest(&self) -> ~str {
let mut acc = ~"";
for &byte in self.digest.iter() {
acc.push_str(fmt!("%02x", byte as uint));
}
acc
}~[T] is just a vector.
There's also the advantage that there's no way to conflict with the * multiplication operator.
~ is an owned pointer (like unique_ptr in C++); & is a "reference" or borrowed pointer (allowing access to data without taking ownership); * is a raw/unsafe pointer, and is identical to C's pointers.
Also, the * pointer doesn't conflict with multiplication because they can never occur in an ambiguous context:
foo * bar = NULL; // C
let bar: *Foo = std::ptr::null(); // Rust
let bar = std::ptr::null(); // Rust, using type inference
struct Digest { digest: ~[u8] } // what's ~ here?
for self.digest.iter().advance |&byte| { acc = acc.append(fmt!("%02x", byte as uint)); } acc
What does the above mean? acc as a separate line and nothing else
I hate it when people reinvent the same construct that can be found in other languages but differently. I guess they want to do something different in their language, is it?