Confusing about “temporarily downgraded” from mutable to read-only
rust-book.cs.brown.edu·17h·
Discuss: r/rust
Flag this post

References and Borrowing

Ownership, boxes, and moves provide a foundation for safely programming with the heap. However, move-only APIs can be inconvenient to use. For example, say you want to read some strings twice:

In this example, calling greet moves the data from m1 and m2 into the parameters of greet. Both strings are dropped at the end of greet, and therefore cannot be used within main. If we try to read them like in the operation format!(..), then that would be undefined behavior. The Rust compiler therefore rejects this program with the same error we saw last section:

error[E0382]: borrow of moved value: `m1`
--> test.rs:5:30
(...rest of the error...)

This move behavior is extremely inconvenient. Programs often need…

Similar Posts

Loading similar posts...