Comments (13)
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.
I do it based on user input. So it's not really too bad program design.
from discord-rs.
@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.
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.
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.
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.
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.
Okay, that is a good workaround. Not as clean as I would like it, but it works.
from discord-rs.
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.
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.
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.
Connection
now implements Drop
. If this works out in practice, I'll close this.
from discord-rs.
Woo! I can't really test it, but it sounds amazing!
from discord-rs.
Related Issues (20)
- Update dependencies HOT 1
- Serialize UserId and ChannelId HOT 2
- Use newer version of websocket HOT 6
- Sharded connections fix HOT 1
- A few deserialization issues HOT 1
- Dj bot: Json error HOT 3
- Cannot connect because of invalid ChannelType HOT 3
- discord invite in readme is expaired
- Multiple packages linking to OpenSSL HOT 2
- how to instantiate LiveServer struct HOT 1
- README Discord invite link is invalid. HOT 1
- cargo.toml doesn't fetch v0.9
- Unable use send_file, "Invalid scheme for Http" HOT 1
- Got Err Decode("Expected string", Null) when trying to call connect()
- Get voice of single players and connect multiple time HOT 3
- there is a token altough it throws 'Expected token: NotPresent' error. HOT 2
- Can't connect because of missing key "guild_positions" HOT 1
- message.content in Event::MessageCreate is always an empty string HOT 1
- Rust websocket https://github.com/ArtemGr/rust-websocket not available HOT 1
- Out-Of-Date & Deprecated Dependencies HOT 6
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 discord-rs.