Hello there, sorry for the newbie question I'm very new to rust but I have made two structs Neuron
and GeneticNetwork
the idea is that GeneticNetwork
contains fields that are type Vec<&Neuron>
this is the code I have so far:
genetic_network.rs
pub struct GeneticNetwork<'a> {
/// a vector containing all input neurons to be used within the network
pub input_neurons: Vec<&'a Neuron<'a>>,
/// a vector containing all internal neurons to be used within the network
pub internal_neurons: Vec<&'a Neuron<'a>>,
/// a vector containing all output neurons to be used within the network
pub output_neurons: Vec<&'a Neuron<'a>>
}
impl<'a> GeneticNetwork<'a> {
pub fn new(input_neurons: Vec<&'a Neuron<'a>>, internal_neurons: Vec<&'a Neuron<'a>>, output_neurons: Vec<&'a Neuron<'a>>) -> GeneticNetwork<'a> {
GeneticNetwork {
input_neurons: input_neurons,
internal_neurons: internal_neurons,
output_neurons: output_neurons
}
}
}
main.rs
fn main() {
let in_n1: &Neuron = &Neuron::new(0.3, NeuronType::Input);
let in_n2: &Neuron = &Neuron::new(0.6, NeuronType::Input);
let int_n1: &Neuron = &Neuron::default();
let int_n2: &Neuron = &Neuron::default();
let o_n1: &Neuron = &Neuron { neuron_type: NeuronType::Output, ..Default::default() };
let o_n2: &Neuron = &Neuron { neuron_type: NeuronType::Output, ..Default::default() };
let input_neurons: Vec<&Neuron> = vec![in_n1, in_n2];
let internal_neurons: Vec<&Neuron> = vec![int_n1, int_n2];
let output_neurons: Vec<&Neuron> = vec![o_n1, o_n2];
let network: GeneticNetwork = GeneticNetwork::new(
input_neurons,
internal_neurons,
output_neurons
);
}
the issue I'm having is the way I'm initializing the vectors, I initialize the neurons first then add them into a vector, this is the only way I could get it working, I wish to do something like this instead:
let input_neurons: Vec<&Neuron> = vec![&Neuron::default(), &Neuron::default()];
but this results in this error:
temporary value dropped while borrowed
consider using a `let` binding to create a longer lived value
sorry if anything is not clear, thanks in advance:)