4 ms·
> Is the borrow syntax (&x) a reference while vanilla (x) is by value? Or are both passed by reference but with different ownership affects? This is briefly ad
by steamer25 10y ago
> Is the borrow syntax (&x) a reference while vanilla (x) is by value? Or are both passed by reference but with different ownership affects?
This is briefly addressed in the FAQs:
"What is the difference between passing by value, consuming, moving, and transferring ownership?
These are different terms for the same thing. In all cases, it means the value has been moved to another owner, and moved out of the possession of the original owner, who can no longer use it. If a type implements the Copy trait, the original owner’s value won’t be invalidated, and can still be used."
https://www.rust-lang.org/en-US/faq.html#what-is-the-difference-between-consuming-and-moving https://www.rust-lang.org/en-US/faq.html#what-is-the-differe...
There are more details in the chapter on the stack and the heap from the book (https://doc.rust-lang.org/book/the-stack-and-the-heap.html https://doc.rust-lang.org/book/the-stack-and-the-heap.html)...
"The stack is very fast, and is where memory is allocated in Rust by default."
"What do other languages do?
Most languages with a garbage collector heap-allocate by default. This means that every value is boxed. There are a number of reasons why this is done, but they’re out of scope for this tutorial. There are some possible optimizations that don’t make it true 100% of the time, too. Rather than relying on the stack and Drop to clean up memory, the garbage collector deals with the heap instead."
So unless your data structure is boxed, it's allocated on the stack and passed by value and different ownership effects apply as well.
> I got hung up the other day on passing a String to a function that accepted &str which someone explained to me Strings dereference to &str but I think I just ended up more confused.
String literals de-sugar to &str. E.g.,
fn borrow_str(s: &str) {}
borrow_str("foo"); // This works
borrow_str(String::new()) // this doesn't work
fn take_str(s: String) {}
take_str("foo") // Doesn't work
take_str(String::new()) // works
take_str("foo".to_owned()) // works
- steveklabnik 10y agoTiny bit: string literals don't desugar to str, that is their actual type. &String will coerce to &str thanks to Deref coercions.