Giter Club home page Giter Club logo

Comments (3)

bluss avatar bluss commented on August 23, 2024

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.

mikepurvis avatar mikepurvis commented on August 23, 2024

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.

bluss avatar bluss commented on August 23, 2024

The academic solution is scopeguard::guard(&mut preamble

Innovations welcome, in this crate or elsewhere 🙂

from scopeguard.

Related Issues (14)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.