Am 05.03.2014 03:54, schrieb Fernando Pelliccioni:
>
> So, now, what is the difference between...
>
> // rust
> let i = ~1234;
>
> and
>
> // C++
> auto i = make_unique<int>(1234);
>
> ?
>
> The Rust code is shorter, but perhaps, more illegible. I think it is
> matter of taste.
>
> But now, the real advantage is not as great as before, or not?
Except that Rust will catch use-after-move at compile time, while C++
will segfault at runtime.
Try something like this:
let i = ~1234;
let j = i; // i moves into j
println!("{}", *j); // this is OK
println!("{}", *i); // tries to print `i` . Compile ERROR
This will not compile in Rust!
Try the same in C++:
auto i = make_unique<int>(1234);
auto j = std::move(i);
cout << *j << endl; // 1234
cout << *i << endl; // Segmentation Fault
Maybe there are warnings generated by C++ which detect this
use-after-move... the point is that unique pointers in C++ are more a
library add-on rather than built into the language.
> I think there is a bad intension of the author of the article to enlarge
> the advantage.
>
>
> Question, How to write the following in Rust using the operator ~ ?
>
> auto e = make_unique<Employee>("Peter");
In Rust this will be:
let e = ~Employee {name: ~"Peter"};
> ...and, what about shared-ownership?
I am not using shared ownership very often, but this should work:
let e = @Employee {name: ~"Peter"};
But I think this will change towards using either Gc<Employee> or
Rc<Employee> so you have the choice between reference counting and
(thread local) garbage collection.
> If the author informs all this, I would think that there is no bad
> intention, but he doesn't, and he uses the ugliest C++ code possible.
You interpret this as bad intention but I am sure there was no bad
intention involved by the author of the article. It's always difficult
to show off the advantages of new languages without hurting the feelings
of users of the "old" language.
I love both Rust and C++, but for C++ this is more a love-hate
relationship :)
Regards,
Michael
_______________________________________________
Rust-dev mailing list
Rust-dev@mozilla.org
https://mail.mozilla.org/listinfo/rust-dev