How do I return a WeightedChoice from a function?

Snowball

I'd like to make a function that takes a parameter x: uint returns a WeightedChoice with options 'a' and 'b' of weights x and 1u, respectively. How can I do that?

This is obviously wrong, but just to give an idea of what I'm trying for:

use std::rand::distributions::{WeightedChoice, Weighted};
fn make_wc<'a>(x: uint) -> WeightedChoice<'a, char> {
    let mut items = [Weighted { weight: x, item: 'a' },
                     Weighted { weight: 1, item: 'b' }];
    WeightedChoice::new(???)
}

The part that's confusing me is the lifetime parameter. As far as I know, I can't create anything of lifetime 'a inside the function, but the argument of WeightedChoice::new needs to have type &'a mut [Weighted<char>].

Vladimir Matveev

Yes, you're right, it is impossible to create anything of lifetime specified as a parameter to a function.

It is possible to return WeightedChoice<'static, char> but it would require using &'static mut [Weighted<T>] which is not appropriate for your use case (if possible at all).

What you can do is "encapsulate" WeightedChoice data into your own struct which would have methods for generating WeightedChoice:

use std::rand::distributions::{Weighted, WeightedChoice};

struct OwningWc<T> {
    items: Vec<Weighted<T>>
}

impl<T: Clone> OwningWc<T> {
    #[inline]
    pub fn new(items: Vec<Weighted<T>>) -> OwningWc<T> {
        OwningWc { items: items }
    }

    #[inline]
    pub fn get(&mut self) -> WeightedChoice<T> {
        WeightedChoice::new(self.items[mut])
    }
}

And then you would use it like this:

fn make_wc(x: uint) -> OwningWc<char> {
    let items = vec![Weighted { weight: x, item: 'a' },
                     Weighted { weight: 1, item: 'b' }];
    OwningWc::new(items)
}

let owc = make_wc(10);
let wc: WeightedChoice<char> = owc.get();

If you don't need genericity, you can optimize OwningWc struct to have, say, [Weighted<char>, ..2] field instead of Vec<Weighted<T>>. You won't even need dynamic allocation in this case.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How do I return value from this function

How do I return a &Path from a function?

How do I return text from a function?

how do I exit (return) from a function inside a another function

How do I return a react component inline from a function?

How do I return an array from an OpenOffice Basic function?

How do I return an array from a vba function in Excel?

How do I return a map from a function in XSLT 3.0?

How do I return a range view from a function?

How do I return formatted string from a function?

How do I return a string from a function that is inside a for loop?

How do I return/yield a value from a recursive function in Elixir?

How do I return the result of a RETURNING clause from a plpgsql function?

How do I return a value from a function triggered by Timer

How do I return a failure value from a bash function?

How do I return different iterators from a function?

How do I return an input tag from a function?

How do I return an owned array from a function?

How do I return a Filter iterator from a function?

How do I return from a recursive generator function in JavaScript?

How do I return data from a function in node?

How do I return a vector element from a Rust function?

How do I return jsx from an outside function with callback?

How do I return an array of struct from a function?

How do I return a value from a promise to my calling function?

How do I return a row constructor from a PL/pgSQL function?

How do I return a modified matrix from a function?

How do I return a value from a function in JavaScript to a textbox in HTML

How do I return a value from a second function in PHP?