I was trying out a simple function when I got this compiler error. What is the actual reason behind this? In my case (following code), writing the type parameter before the lifetime parameter has no effect. Shouldn't the compiler know better?
use std::fmt::Debug;
fn random_func<T : Debug, 'a>(parameter : &'a mut T) {
println!("{:?}", parameter);
}
fn main(){
let mut name : String = "random".to_string();
random_func(&mut name);
println!("{:?}", "compiled successfully");
}
error:
life_time_trait.rs:3:27: 3:29 error: lifetime parameters must be declared prior to type parameters
life_time_trait.rs:3 fn random_func<T : Debug, 'a>(parameter : &'a mut T) {
^~
I'm not sure this is the reason, but remember that type parameters can have lifetime bounds, but lifetime parameters can't have type bounds. So putting them first means you don't need to deal with non-forward declarations.
Personally, I think this rule makes things a bit easier to understand, and a bit easier to implement. Win-win!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With