I am writing a very simple recursive program for finding all prime numbers between two numbers:
use std::cmp::PartialOrd; use std::ops::{Add, Div, Rem, Sub}; fn _is_prime<T>(n: T, dividend: T, one: T) -> bool where T: Copy + Rem<Output = T> + Sub<Output = T> + PartialOrd, { if dividend == one { true } else { if n % dividend < one { false } else { _is_prime(n, dividend - one, one) } } } fn _primes_between<'a, T>(a: T, b: T, one: T, v: &'a mut Vec<T>) -> &'a mut Vec<T> where T: Copy + Rem<Output = T> + Add<Output = T> + Sub<Output = T> + PartialOrd, { if a <= b { if _is_prime(a, a - one, one) { v.push(a); } _primes_between(a + one, b, one, v) } else { v } } fn primes_between<T>(a: T, b: T) -> Vec<T> where T: Copy + Div<Output = T> + Rem<Output = T> + Add<Output = T> + Sub<Output = T> + PartialOrd, { let one = a / a; let mut v: Vec<T> = Vec::new(); *_primes_between(a, b, one, &mut v) } fn main() { primes_between(3, 13).iter().for_each(|i| println!("{}", i)); } The problem is:
error[E0507]: cannot move out of a mutable reference --> src/main.rs:42:5 | 42 | *_primes_between(a, b, one, &mut v) | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ move occurs because value has type `std::vec::Vec<T>`, which does not implement the `Copy` trait How do I solve that error?
_prefix is not necessary for privacy because there is thepubkeyword, and so it has acquired the meaning of "unused". A seasoned Rustacean wouldn't name functions things like_primes_betweenand_is_prime. Besides usingpub, another thing you can do to limit visibility is declare functions inside other functions, if they are not used elsewhere.