Spurious "Use of moved value" on a u32
See original GitHub issueEnvironment
- Plugin 0.2.114.2151-193
- Rust 1.40.0
- CLion 2019.3
- Windows
Problem description
I have code that imports std::u32 to get at the u32::MAX const, but it seems to generate a spurious “use of moved value” when I use the same u32 twice in a function:
Steps to reproduce
In my code base, the word first
on the line under “next: first,” is underlined and marked with error “use of moved value”. I assume the plugin sees that I used it on the line previous, thinking it was a move, but as it is a u32 so the compiler would done a copy not a move.
use std::u32;
#[derive(Debug, Clone)]
pub struct Handle {
next: u32,
first: u32,
}
impl Handle {
/// Creates a new handle factory, that starts with the supplied number
pub fn new(first: u32) -> Handle {
Handle {
next: first,
first, // <-- ERROR HERE
}
}
/// Returns the next handle to be issued, internally incrementing each time so the handle
/// is always different until it wraps back to the start.
pub fn next(&mut self) -> u32 {
let next = self.next;
// Increment next
if self.next == u32::MAX {
self.next = self.first;
} else {
self.next += 1;
}
next
}
pub fn set_next(&mut self, next: u32) {
self.next = next;
}
/// Resets the handle to its initial state
pub fn reset(&mut self) {
self.set_next(self.first);
}
}
Issue Analytics
- State:
- Created 4 years ago
- Comments:5 (2 by maintainers)
Top Results From Across the Web
What do I have to do to solve a "use of moved value" error?
When a function requires a parameter by value, the compiler will check if the value can be copied by checking if it implements...
Read more >A hammer you can only hold by the handle
Note that use_name takes name 's ownership (and it's not pass-by-reference) so name is moved on line 5, and it cannot be used...
Read more >Rust for Embedded C Programmers | OpenTitan Documentation
Reference lifetimes start when the reference is taken, and end either when the lifetime goes out of scope or when the value referenced...
Read more >A hammer you can only hold by the handle - Andrea Lattuada
Note that use_name takes name 's ownership (and it's not pass-by-reference) so name is moved on line 5, and it cannot be used...
Read more >How to solve this "used of moved value" issue - help
Hi, I am new to Rust. Currently I'm confused about this kind of problem. My code may look like this: pub fn test(mut...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
No I don’t. It’s a stable rust set through “rustup default stable-gnu”. I’ll get back to you tonight because one machine can’t fetch from crates.io at the moment and I want to reproduce the behaviour from a machine that is building normally.
I’m going to close the issue because it works on the other computer. I think it is because the external crates could not be fetched there was nothing in External Libraries, even a reference to Rust’s own stdlib.