Comments (3)
defer is using a closure, you can use a macro less version of that to see it better - but it has the borrow checker errors as any closure would have. The version of the scope guard that takes and owns a value exists to solve this problem (see examples in docs).
from scopeguard.
Hey, thanks for the response. Based on the example in the docs, I made it to here:
fn find_number(preamble_len: usize, numbers: &[u64]) -> Option<u64> {
let mut iter = numbers.iter();
let mut preamble: VecDeque<&u64> = iter.by_ref().take(preamble_len).collect();
for value in iter {
let mut preamble2 = scopeguard::guard(preamble, |mut preamble| {
preamble.pop_front();
preamble.push_back(value);
});
if preamble2.iter().copied().combinations(2)
.any(|c| c.iter().copied().sum::<u64>() == *value) {
continue;
}
return Some(*value);
}
None
}
However, I think this situation is more complex than what you show in the doc, because the value being mutated is outside the scope that I want to exit on, so I'm getting a bunch of value moved on previous iteration type issues.
It seems pretty clear that the complexity required here isn't worth it, but as a purely academic exercise, I'd be delighted to figure out how to make this work.
Here's a playground link if you're interested to experiment: https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&gist=c4407108eabe72ab21604d7c09d43159
from scopeguard.
The academic solution is scopeguard::guard(&mut preamble
Innovations welcome, in this crate or elsewhere
from scopeguard.
Related Issues (14)
- Could you please update it to Rust 1.17? HOT 7
- Can ScopeGuards be returned? HOT 6
- Document why guard closure is FnMut, not FnOnce HOT 2
- New features HOT 3
- Generating scope/transaction guards? HOT 1
- Add github actions for CI HOT 3
- Is it able to support async calling? HOT 4
- Add some `Future`-friendly helpers? HOT 3
- Support for Try in defer? HOT 1
- Use libcore instead of libstd to allow inclusion in embedded projects. HOT 1
- New release? HOT 3
- #[inline(always)] attribute for deref() and deref_mut()?
- Why store a value when the closure could just capture it? HOT 2
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 scopeguard.