Converting a normal funciton to a generic function
⚓ Rust 📅 2025-05-07 👤 surdeus 👁️ 11The code below works for me but if I remove the comments around my generic version of push_3, I get the compile error.
It seems to me that rust is complaining that 3.0 is not of the proper type in the generic function but it works fine for the non-generic version and I do not understand why ?
use std::cell::RefCell;
use std::ops::DerefMut;
fn push_3(v : &mut Vec<f32>) {
v.push( 3.0 );
}
/*
fn generic_push_3<T>(v : &mut Vec<T>) {
v.push( 3.0 );
}
*/
fn main() {
let v = RefCell::new( vec![1.0, 2.0] );
push_3( v.borrow_mut( ).deref_mut() );
println!( "{:?}", v );
}
6 posts - 5 participants
🏷️ rust_feed