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>]
.
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.
Comments