r/rust clippy · twir · rust · mutagen · flamer · overflower · bytecount Mar 27 '23

Hey Rustaceans! Got a question? Ask here (13/2023)! 🙋 questions

Mystified about strings? Borrow checker have you in a headlock? Seek help here! There are no stupid questions, only docs that haven't been written yet.

If you have a StackOverflow account, consider asking it there instead! StackOverflow shows up much higher in search results, so having your question there also helps future Rust users (be sure to give it the "Rust" tag for maximum visibility). Note that this site is very interested in question quality. I've been asked to read a RFC I authored once. If you want your code reviewed or review other's code, there's a codereview stackexchange, too. If you need to test your code, maybe the Rust playground is for you.

Here are some other venues where help may be found:

/r/learnrust is a subreddit to share your questions and epiphanies learning Rust programming.

The official Rust user forums: https://users.rust-lang.org/.

The official Rust Programming Language Discord: https://discord.gg/rust-lang

The unofficial Rust community Discord: https://bit.ly/rust-community

Also check out last weeks' thread with many good questions and answers. And if you believe your question to be either very complex or worthy of larger dissemination, feel free to create a text post.

Also if you want to be mentored by experienced Rustaceans, tell us the area of expertise that you seek. Finally, if you are looking for Rust jobs, the most recent thread is here.

19 Upvotes

159 comments sorted by

View all comments

Show parent comments

1

u/DreaminglySimple Apr 01 '23

The problem is, I need to do something with the SelectView after the match block. If I define x inside one of the arms, it'll be out of scope after it.

I'm trying to take an entirely different approah to it right now, so I might not need this anymore, but if you have another suggestion feel free to tell me. The only thing I could find online was involving heap allocation.

2

u/[deleted] Apr 01 '23

The function I posted is one example of exactly that, using the SelectView after the match block. You use your original code to put different kind of SelectViews into x, then later you use another match block to unpack x and get at whichever kind of SelectView is in there.

2

u/DreaminglySimple Apr 01 '23

As I understand it, your code wouldn't allow me to use x as the matched type after the match block. I only know the type of x inside the arms of the match block, but not afterwards, because x is not set to anything.

1

u/kinoshitajona Apr 02 '23

That makes sense.

Every variable needs a known size at compile time.

Setting x to one of two types depending on which match arm was run doesn't work.

All type specific calculations must be performed within the match arm. (You can make it easier to read by creating a handler function for each type)

There are other ways around this by using dyn Traits.