Comments (4)
@rustbot label -needs-triage +T-lang -C-bug +C-discussion
Not sure if this is a bug because this is clearly documented (https://doc.rust-lang.org/book/ch05-01-defining-structs.html):
Note that the struct update syntax uses = like an assignment; this is because it moves the data, just as we saw in the “Variables and Data Interacting with Move” section. ... If we had given user2 new String values for both email and username, and thus only used the active and sign_in_count values from user1, then user1 would still be valid after creating user2. Both active and sign_in_count are types that implement the Copy trait, so the behavior we discussed in the “Stack-Only Data: Copy” section would apply.
from rust.
I think "clearly documented" is overstating it a bit. I've read the documentation you linked before filing this and I found that in this case it wasn't conclusive. In particular:
Note that the struct update syntax uses
=
like an assignment; this is because it moves the data
The implication is "therefore you can use the fields anymore unless they're Copy
", but it seems like "you can't use the fields anymore unless they're Copy
or can be reborrowed" would be a valid interpretation too.
This part especially:
If we had given
user2
newString
values for bothusername
, and thus only used theactive
andsign_in_count
values fromuser1
, thenuser1
would still be valid after creatinguser2
. Bothactive
andsign_in_count
are types that implement theCopy
trait, so the behavior we discussed in the “Stack-Only Data: Copy” section would apply.
Implies that the struct itself isn't moved, fields are moved, and the struct is still valid if the only field accessed can be moved without invalidation. The doc mentions Copy, but it doesn't explicitly opts out of reborrowing.
(Yes, I'm splitting hairs on all this. My more down-to-earth takeaway is that, as an end-user, this feels more like a corner case not being handled than a new feature not being added yet.)
from rust.
Ah sorry, yeah I agree it's quite inconclusive for this particular case. Hope someone more experienced with the language semantics can help sort it out for you.
@rustbot label +C-bug
from rust.
As @scottmcm has noted before, the current semantics for the "functional record update" syntax... the JS programmer in me wants to just call it "spread syntax", tbh... are actually Quite Bad, and should probably be more intuitive (in the sense of "they should work more like a move/reborrow"... a destructure, then a restructure, I guess?). I believe this is consistent with the current bad semantics, however.
from rust.
Related Issues (20)
- Use of match ergonomics affect borrow checking HOT 2
- Blanket, recursive impls cause bad compiler suggestions
- Chains of checked_add could get better codegen
- Review of Reference change regarding receivers and object safety HOT 1
- `Hash` implementation for `Path` ignores path separators HOT 4
- rust panic HOT 1
- Decide on Rule 4 variant for match ergonomics HOT 1
- opt-dist: separate dist-build from profiling HOT 4
- "SIGSEGV: invalid memory reference" when compiling with optimizations HOT 3
- Assigning the wrong fn type to a variable mentions 'pointer' vs 'item'
- ICE: `write_immediate_to_mplace: invalid Scalar layout:` with feature `const_mut_refs` and `const_refs_to_static`
- ICE: called `Option::unwrap()` on a `None` value HOT 1
- ICE: `type mismatch when copying!` HOT 1
- Can't use `vmsr` instruction in `global_asm!` on `armv7r-none-eabihf` without `codegen-units=1` HOT 7
- `tarpaulin` and check-cfg HOT 4
- panic: "failed to open bitcode file" when changing `-Ccodegen-units` in successive compiles.
- VecDeque::split_off has unexpectedly poor performance characteristics when splitting off from the front.
- Unhelpful `.clone()` suggestion when moving a mutable reference HOT 1
- Miscompilation on release profile when `std::ptr::read` is used to cast byte primitive to some tuples in unreachable code paths HOT 4
- Tracking Issue for PathBuf::add_extension and Path::with_added_extension
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from rust.