Converting a normal funciton to a generic function

⚓ rust    📅 2025-05-07    👤 surdeus    👁️ 5      

surdeus

Warning

This post was published 55 days ago. The information described in this article may have changed.

The 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

Read full topic

🏷️ rust_feed