97 karma · joined Oct 10, 2013 (13y ago) · 12 submissions
comment 1mo ago · link · parent
var words: u8[][>..] = [];
for i in 5 { words.push(str("word", i)); }
In C++ that is a vector<string>: one heap allocation for the vector,
one per string, a pointer and a capacity per element, and a walk over
all of them when it goes out of scope.
The example you've provided uses strings that are eligible for small string optimization, which avoids per-string allocation. All mainstream STL implementations perform this optimization. You can read about several implementations here: https://devblogs.microsoft.com/oldnewthing/20230803-00/?p=10...comment 6mo ago · link · parent
comment 2y ago · link · parent
The documentation addresses this case specifically: https://doc.rust-lang.org/stable/std/os/unix/io/index.html#p...
"Rust’s safety guarantees only cover what the program itself can do, and not what entities outside the program can do to it. /proc/self/mem is considered to be such an external entity..."
comment 3y ago · link · parent
comment 6y ago · link · parent
If you have a vector v=(1,0) that points to the right, you can scale this vector infinitely in that direction by multiplying it by a positive scalar.
5v = (5,0)
62.1v = (62.1,0)
Similarly, you can scale that vector infinitely in the opposite direction (i.e. left) by multiplying it by a negative scalar:
-987v = (-987,0)
If we call this scalar c, the expression cv allows us to represent any point along the X axis simply by varying c, meaning that cv defines a line along that axis.
Similarly, we can do the same for a vector w=(0,1) along the Y axis, scaling it by d.
Now we have a method for moving to any point on the XY plane simply by varying c and d in the linear combination: cv + dw, meaning that we've defined a plane using two vectors.
Two caveats:
- this won't work if v and w are parallel; for example, if v = -w (and neither are zero) then we can only move along a line instead of a plane
- it also won't work if either of the vectors are zero, because no matter what you multiply by, a zero vector can only represent a single point
9.
John Frum and the Cargo Cults (2007)(damninteresting.com ↗)
1 points · by jcmoyer · 8y ago · discuss
10.
Guaranteeing memory safety in Rust [video](air.mozilla.org ↗)
53 points · by jcmoyer · 12y ago · 26 comments
comment 12y ago · link · parent
I would say that Rust's defining feature is the borrow checker[1], which eliminates an entire category of pointer related errors at compile time.
>3 - A lot of things are renamed. auto->let, new->box, switch->box You get the feeling that effort was put in to make the language explicitly look different from C++
Rust is strongly influenced by the ML family of languages. I believe that's where some of the keywords came from.
>4 - the Rust switch statement don't fall through... This one was truly mind blowing. The one useful feature of switch statement got ripped it out! If you don't really need the fall through, I'd just avoid using them completely...
Rust doesn't have a classical switch statement. Instead, it has a 'match' keyword that performs pattern matching on the input so you can easily destructure a complicated blob of data into more manageable pieces. This is also a concept borrowed from the ML family. If you really wanted to, you could write a macro that emulates the C style switch statement.
>5 - I've never really seen an equivalent to boost (in combination to the STL) in other languages (maybe I didn't look hard enough). Could you maybe make a post about the RUST standard library? Libraries are always the deal breaker
There's a great overview[2] of what the standard distribution contains on the Rust website.
[1] http://static.rust-lang.org/doc/master/rustc/middle/borrowck...
[2] http://static.rust-lang.org/doc/master/index.html#libraries
comment 12y ago · link · parent