-
-
Notifications
You must be signed in to change notification settings - Fork 14.4k
Description
Section 6 of The Rust Pointer Guide is as follows:
In many languages with pointers, you'd return a pointer from a function
so as to avoid copying a large data structure. For example:struct BigStruct { one: int, two: int, // etc one_hundred: int, } fn foo(x: Box<BigStruct>) -> Box<BigStruct> { return box *x; } fn main() { let x = box BigStruct { one: 1, two: 2, one_hundred: 100, }; let y = foo(x); }The idea is that by passing around a box, you're only copying a pointer, rather
than the hundredints that make up theBigStruct.This is an antipattern in Rust. Instead, write this:
struct BigStruct { one: int, two: int, // etc one_hundred: int, } fn foo(x: Box<BigStruct>) -> BigStruct { return *x; } fn main() { let x = box BigStruct { one: 1, two: 2, one_hundred: 100, }; let y = box foo(x); }This gives you flexibility without sacrificing performance.
You may think that this gives us terrible performance: return a value and then
immediately box it up ?! Isn't that the worst of both worlds? Rust is smarter
than that. There is no copy in this code. main allocates enough room for the
`box , passes a pointer to that memory into foo as x, and then foo writes the
value straight into that pointer. This writes the return value directly into
the allocated box.This is important enough that it bears repeating: pointers are not for
optimizing returning values from your code. Allow the caller to choose how they
want to use your output.
I was very confused by the second to last paragraph of this section. The paragraph uses the word "that" when referencing a memory location and a pointer. The first reference is clear, but the second isn't. This isn't helped by the fact that in the example the label x is used for both the variable passed into the function foo() and the parameter inside the function, making it hard to clearly express where and when values are being assigned. It also says the same thing twice
The disambiguation should make it clear that both the pointer to x and the pointer to y are being used in the function, where x is explicity passed as a parameter and y is implicitly passed as the return location. The general idea the paragraph expresses is already communicated, but I think it could be more explicit about the mechanics. Currently it almost makes it sound like foo() is returning a value via the passed parameter, or that it is moving the value rather than copying from one heap location to another.
Also someone mentioned that the "box" in the second paragraph may be missing another "".