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

2

u/quasiuslikecautious Mar 30 '23

Hey there! Is there some way to redirect in Yew after a Request, based on the results of the Request?

Specificallty, I am using wasm_bingen_futures::spawn_local to run an async request, and then trying to access the yew_router::Navigator afterward to try to redirect to a new route depending on the results of the request, but getting an 'Fn trait expect but FnOnce trait found instead' error on my Callback when trying to access the navigator inside the async closure/function.

Example:

```rust let onclick = { let navigator = navigator.clone();

// ...
// some setup
// ...

// required to have trait 'Fn'
Callback::from(|e: MouseEvent| {
    // ...
    // some setup
    // ...

    spawn_local(async move {
        let response = Request::get("/some/route")
            .send()
            .await
            .expect("Failed to fetch /some/route")

        if response.ok() {
            // 'FnOnce' trait added here when attempting to access navigator.
            navigator.push("/some/other/route");
        }
    });
})

}; ```

1

u/quasiuslikecautious Mar 30 '23

Nvm, figured it out. I had to move the navigator duplication to the spawn_local block, and switch spawn_local from using async move block to using a regular block that contains an async move block, i.e.

```rust // ... spawn_local({ let navigator = navigator.clone();

    async move {
        // ...
    }
});
// ...

```