Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Another rust generics exercise

Tags:

rust

I'm trying to implement a Monad like trait in Rust. Mostly just for fun and to get familiar with the type system. I'm pretty sure I will not be able to fully implement a Monad trait due to the lack of "higher kinds" as explained in this reddit discussion, but I want to see how close I can get. For some reason I can't get this code to compile. Seems like it should. Can someone explain why?

trait Monad<T> {
    fn lift(val: T) -> Self;
}

struct Context<T>{ 
    val: T 
}

impl<T> Monad<T> for Context<T> {
    fn lift(x: T) -> Context<T> {
        Context{val: x}
    }
}

fn main() { 
    let c:Context<int> = Context<int>::lift(5i);
}
like image 613
MFlamer Avatar asked May 26 '13 23:05

MFlamer


1 Answers

Static methods defined in a trait must be called through it. So, you'd have:

let c: Context<int> = Monad::lift(5);
like image 195
Luqman Avatar answered Oct 14 '22 07:10

Luqman