3

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?

1
  • 1
    With the landing of github.com/mozilla/rust/pull/10576 , Gc<T> is replacing the @ syntax, but adding #[feature(managed_boxes)] in top of your code should make that error go. Commented Dec 26, 2013 at 22:03

1 Answer 1

3

You need the @'s because extra::list::List is defined such that Cons requires an @List<T> as the second value.

However, as you found out, trying to add @'s throws a feature gate error. As snf commented, you can add

#[feature(managed_boxes)];

to the top of the root crate file to enable the usage of @.

Sign up to request clarification or add additional context in comments.

2 Comments

Are "managed_boxes" an old feature going away or a new feature coming in?
@T.Stone: They're an old feature going away. They're being replaced with library types std::gc::Gc<T> and std::rc::Rc<T>.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.