The Rust Language
I've been writing in Rust a couple days and I can say that Rust is a nice language. Thanks to its ownership and borrow mechanisms, the semantics of a snippet of code can be very clear. And that's also one thing I like about Go, as compared to C++, in that the semantics of the code can't be mistaken as the language is so simple.
But at the current version (rustc 1.19.0), there is an ergonomic issue regarding the match expression and borrow checker, that I want resolved before I really start using the language.
It is represented by this issue rfc#811 and I hope it would be resolved soon.
The issue I encountered is: I want to look inside an enum value and move the value in certain condition, possibly to pack it in an Error object and pass it up. In order to use an enum type value you have to go through an match expression one way or other.
So here is me trying my hand at a rudimentary assembler, ...again.
Oh no. That arm of the match expression (&Expr::Identifier) coincides with the lifetime of matched binding (ref s). So you can't move e, based on the analysis of e, without some additional, pointless elaboration. I say pointless because the lifetime of borrow doesn't need to last the whole match clause, but only up to the minimum extent of the borrow.
I believe this can be easily get around by wrapping the Exprs in Rc pointers and share them around, which is a more sensible design anyway. I'll do that another time.
But at the current version (rustc 1.19.0), there is an ergonomic issue regarding the match expression and borrow checker, that I want resolved before I really start using the language.
It is represented by this issue rfc#811 and I hope it would be resolved soon.
The issue I encountered is: I want to look inside an enum value and move the value in certain condition, possibly to pack it in an Error object and pass it up. In order to use an enum type value you have to go through an match expression one way or other.
So here is me trying my hand at a rudimentary assembler, ...again.
enum Expr {
Identifier { value: String, token: Token },
Integer { value: i32, token: Token },
List { exprs: Vec<Expr> },
}
let e = get_an_expr();
match &e {
&Expr::Identifier { value: ref s, .. } => {
let symbol = symtab.get(s).ok_or(TranslateError::UnknownSymbol(e))?; // error[E0505]: cannot move out of `e` because it is borrowed
....
}
_ => { ... }
}
Oh no. That arm of the match expression (&Expr::Identifier) coincides with the lifetime of matched binding (ref s). So you can't move e, based on the analysis of e, without some additional, pointless elaboration. I say pointless because the lifetime of borrow doesn't need to last the whole match clause, but only up to the minimum extent of the borrow.
I believe this can be easily get around by wrapping the Exprs in Rc pointers and share them around, which is a more sensible design anyway. I'll do that another time.
Comments
Post a Comment