Thanks to everyone who replied. Daniel Micay gave me more ideas on the
irc channel about the move semantics. Here is the final program which
compiled and worked fine (with borrow/clone) is given below for
reference. Additionally I later found Patrick Walton's blog post
explaining reference counting and why it is going to be in standard
library etc. Very nice stuff, I am learning a lot. :) Thanks.

Ramakrishnan

use std::rc::Rc;

#[deriving(Clone)]
enum Expr
{
    Num(int),
    Add(Rc<Expr>, Rc<Expr>),
}

fn eval(e: Expr) -> int {
    match e {
        Num(x) => x,
        Add(x, y) => { let xx = x.borrow();
                       let yy = y.borrow();
                       eval(xx.clone()) + eval(yy.clone())
        },
    }
}

fn main() {
    println(format!("eval 2 = {:d}", eval(Num(2))));
    println(format!("eval 2 + 3 = {:d}", eval(Add(Rc::new(Num(2)),
Rc::new(Num(3))))));
}

On Tue, Oct 29, 2013 at 12:35 AM, Oren Ben-Kiki <[email protected]> wrote:
> Right, it would have to be @mut Expr to allow for cycles... and you'd have
> to really work hard to construct the cycle. At any rate, this isn't what
> you'd want - very probably just using ~Expr would be enough.
>
>
> On Mon, Oct 28, 2013 at 8:29 PM, Daniel Micay <[email protected]> wrote:
>>
>> On Mon, Oct 28, 2013 at 2:27 PM, Oren Ben-Kiki <[email protected]> wrote:
>>>
>>> If you use ~Expr instead of @Expr, then the expressions would have to
>>> form a tree (A = B + C). If you use Rc<Expr>, you could build a DAG (A = B +
>>> B). With @Expr (if that worked), in principle you could allow for cycles (A
>>> = B + A), which is probably not what you want.
>>
>>
>> You won't actually be able to create cycles with @T unless T is non-Freeze
>> because it's immutable and Rust doesn't use laziness.
>
>
>
> _______________________________________________
> Rust-dev mailing list
> [email protected]
> https://mail.mozilla.org/listinfo/rust-dev
>



-- 
  Ramakrishnan
_______________________________________________
Rust-dev mailing list
[email protected]
https://mail.mozilla.org/listinfo/rust-dev

Reply via email to