I'm just starting in Rust and trying to write some basic algorithms to get a feel for the language. My attempt here is to write a function that given an int will convert it into binary form represented by a linked list of bool values with true for 1 and false for 0 (if there's a better "bit" type I'm open to that suggestion as well).
My current implementation is as follows:
fn to_base_2(num: int) -> List<bool> {
fn to_base_2_acc(num: int, acc: List<bool>) -> List<bool> {
if num == 0 {
return acc;
} else {
let bit = (num % 2) == 0;
let new_acc = Cons(bit, acc);
return to_base_2_acc(num / 2, new_acc);
}
}
to_base_2_acc(num, Nil);
}
This code fails to compile with:
main.rs:20:28: 20:31 error: mismatched types: expected `@extra::list::List<bool>` but found `extra::list::List<bool>` (expected @-ptr but found enum extra::list::List)
main.rs:20 let new_acc = Cons(bit, acc);
^~~
However updating the code to be managed by dropping @ in front of List results in the following:
main.rs:15:34: 15:45 error: The managed box syntax is being replaced by the `std::gc::Gc` and `std::rc::Rc` types. Equivalent functionality to managed trait objects will be implemented but is currently missing.
main.rs:15 fn to_base_2_acc(num: int, acc: @List<bool>) -> List<bool> {
^~~~~~~~~~~
main.rs:15:34: 15:45 note: add #[feature(managed_boxes)] to the crate attributes to enable
main.rs:15 fn to_base_2_acc(num: int, acc: @List<bool>) -> List<bool> {
^~~~~~~~~~~
main.rs:25:21: 25:25 error: The managed box syntax is being replaced by the `std::gc::Gc` and `std::rc::Rc` types. Equivalent functionality to managed trait objects will be implemented but is currently missing.
main.rs:25 to_base_2_acc(num, @Nil);
^~~~
main.rs:25:21: 25:25 note: add #[feature(managed_boxes)] to the crate attributes to enable
main.rs:25 to_base_2_acc(num, @Nil);
^~~~
This is using rustc 0.9-pre. Do linked lists not work in this version of the compiler?
@syntax, but adding #[feature(managed_boxes)] in top of your code should make that error go.