2 ms·
Besides that Rust does not do manual memory management (as a peer commenter pointed out), Rusts borrowing rules also prevent typical ownership bugs. For instanc
by danieldk 9y ago
Besides that Rust does not do manual memory management (as a peer commenter pointed out), Rusts borrowing rules also prevent typical ownership bugs. For instance:
- You split a string, returning an iterator or slice where every element is a slice of the original string.
- You change the original string.
- Now the splitting may be invalid.
Such bugs are not possible in Rust, since; (1) the SplitWhitespace struct borrows the str immutably; (2) the borrows checker does not permit simultaneously borrowing data as mutable and immutable.
Another problem that is prevented by Rust is 'memory leaks' where someone splits a large string and uses only a smaller substring. If the substring is a slice of the original string, a GC cannot deallocate the larger string. Rust prevents this, because a string slice reference (&str) cannot outlive the underlying String. [1]
tl;dr: better of the half points are indirectly and indirectly caused by Rust's ownership model that prevents a lot of ownership bugs. That does not mean that you do not have to think about ownership problems in other languages.
[1] Such memory leaks were one of the reasons why Oracle Java switched to a much slower, copying implementation of substring:
http://java-performance.info/changes-to-string-java-1-7-0_06/ http://java-performance.info/changes-to-string-java-1-7-0_06...