Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

You can pass a value that is neither copy or clone, but then it gets moved into the callee, and is no longer available in the caller.

Note that calling by value is expensive for large types. What those other languages do is just always call by reference, which you seem to confuse for calling by value.

Rust can certainly not do what you would prefer. In order to typecheck a function, Rust only needs the code of that function, and the type defitions of everything else, the contents of the functions don't matter. This is a very good rule, which makes code much easier to read.



Is it expensive in Rust? Normally only data in stack gets copied. Heap data is untouched


Yes, but if you have a large value type it will be on the stack unless you manually box it. Passing by value can get quite expensive quite fast, especially if the value keeps being passed up and down the call chain.


Is this really true? What do you mean by value types? The types that implement copy or any struct types? Because I think struct types only get moved


Move and copy are both the same operation under the hood, move just means that the old version is marked invalid and not available for use anymore.

For large structs (or enums), move and copy both compile into memcpy. If the structures are large, this can take a lot of time.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: