Post ATd4IcZeFGzchTvwh6 by jakub_neruda@techhub.social
(DIR) More posts by jakub_neruda@techhub.social
(DIR) Post #ATd4IbNYgjjCzhGmDA by jakub_neruda@techhub.social
2023-03-14T04:19:26Z
0 likes, 0 repeats
#Rust prides itself in how (among others) its ownership semantics differ from #cplusplus. Instead of pass-by-value making copy, it moves the memory inside the function. If you want to copy, you have to do it manually. In C++, you copy by default and move on demand.I think this is just a different way to screw the user. I think the compiler should decide when to move and when to copy, based on how the source variable is used afterwards and just do it on behalf of the user (instead of bitching about it in the error log).C++ compilers cannot do so effectively, but Rust is already designed with use-after-move diagnostic in mind. If it did that, it would eliminate one point of confusion for newcomers and be appropriately effective in terms of generated assembly speed.#programming
(DIR) Post #ATd4Ic0CN4dWvXLdRI by ilmai@hachyderm.io
2023-03-14T13:42:08Z
0 likes, 0 repeats
@jakub_neruda I really don’t agree, I think .clone() is useful documentation for “this might be expensive.” Rust in general prefers explicit to implicit so this change would also be opposed to that.
(DIR) Post #ATd4IcZeFGzchTvwh6 by jakub_neruda@techhub.social
2023-03-14T15:12:32Z
0 likes, 0 repeats
@ilmai Now I understand your point. True, Rust is very explicit (maybe too much for my taste) and I can respect this argument.I would still love this feature in C++ because it has the slow option as the default and it shouldn't be the programmers job to optimize this by hand.One can design their APIs to mandate move like Rust, but that is dangerous until use-after-move is enabled for every C++ compiler as a default error.
(DIR) Post #ATd4Id5uJKnUJX1hya by jakub_neruda@techhub.social
2023-03-14T22:57:16Z
0 likes, 0 repeats
@ilmai Still, there's a catch to being explicit.If you call foo(a: T) multiple times, cloning the parameter first N-1 times and moving it for the last time, it could easily happen that somebody will remove the last call and forgets to also change the last clone to move. Such things happen in production quite easily.Suddenly you have one clone too many, something that would not happen if you let the compiler to optimize it automatically...Maybe I am putting too much trust into the compiler but if C++ taught me anything it is that compiler is always smarter than myself.
(DIR) Post #ATd4IdeIFUIq2B7AZc by ekuber@hachyderm.io
2023-03-15T04:41:52Z
1 likes, 0 repeats
@jakub_neruda @ilmai fwiw, clippy (which is part of the standard Rust tooling, top right under tools in the playground) catches thishttps://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=b593efb219fbca4cfd67ec8491dea165