Coder Social home page Coder Social logo

Comments (13)

Xaeroxe avatar Xaeroxe commented on May 28, 2024

You can create functions that consume values, so you can still use this in a function. If you have a function that optionally consumes a value you might want to rethink your program design.

from discord-rs.

jD91mZM2 avatar jD91mZM2 commented on May 28, 2024

I do it based on user input. So it's not really too bad program design.

from discord-rs.

Xaeroxe avatar Xaeroxe commented on May 28, 2024

@legolord208 This is my best attempt at optional consumption. It's not the prettiest program design but eh it works.

https://play.rust-lang.org/?gist=eabd2bf88686f5e4d614d704c4762821&version=stable&backtrace=0

from discord-rs.

jD91mZM2 avatar jD91mZM2 commented on May 28, 2024

Excuse me - how's the relevance if that? It'd been one thing if it was the same string and you called .into_bytes based on some other string, but...?

from discord-rs.

Xaeroxe avatar Xaeroxe commented on May 28, 2024

It's optional consumption. If food isn't consumed (You didn't choose to shutdown discord) then the string is given back. If it is consumed (You chose to shut down discord) then the value isn't returned instead you get None. String is an owned value so if you don't return string at the end of the function then food will be destroyed.

from discord-rs.

Xaeroxe avatar Xaeroxe commented on May 28, 2024

Basically it's your workaround. This will let you pass in a connection to a function, if the connection isn't closed in the function then you can continue using it in later statements.

from discord-rs.

Xaeroxe avatar Xaeroxe commented on May 28, 2024
fn cookie_monster(food: String) -> Option<String> {
    if food == "cookie" {
        println!("Nom nom nom");
        food.into_bytes();
        None
    } else {
        println!("Eww");
        Some(food)
    }
}

There, this version is a little more explicit about the consumption. Still works the same though.

from discord-rs.

jD91mZM2 avatar jD91mZM2 commented on May 28, 2024

Okay, that is a good workaround. Not as clean as I would like it, but it works.

from discord-rs.

jD91mZM2 avatar jD91mZM2 commented on May 28, 2024

On the other hand, that causes issues with move closures.

I generally think implementing the drop trait would be nice anyways. Imagine how awesome it would be having it clean up on itself without having to worry about it?

from discord-rs.

jD91mZM2 avatar jD91mZM2 commented on May 28, 2024

I like the way tempdir did it. They have two functions. One that does cleanup, and the drop trait which also does cleanup, in case it hasn't already been ran.

This allows optionally seeing the error message, but also always cleaning up.

from discord-rs.

SpaceManiac avatar SpaceManiac commented on May 28, 2024

I want to say there was rationale for not implementing Drop, but I don't remember what it is, so that will probably be the solution.

from discord-rs.

SpaceManiac avatar SpaceManiac commented on May 28, 2024

Connection now implements Drop. If this works out in practice, I'll close this.

from discord-rs.

jD91mZM2 avatar jD91mZM2 commented on May 28, 2024

Woo! I can't really test it, but it sounds amazing!

from discord-rs.

Related Issues (20)

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.