These are chat archives for rust-lang/rust

1st
May 2017
VJ
@00imvj00
May 01 2017 09:04
fn append(&mut self, arg: i32) {
       let mut current_link: &mut Link = &mut self.head; 
       loop {
           match *current_link {
                  Link::More(ref mut node) => {
                     if LinkedList::is_empty(&node) == true { //next node is empty
                        println!("Next node is empty");  
                     }else{
                        current_link = &mut node.next;
                     }
                  },
                  Link::Empty => {
                    println!("Empty got, breaking");
                    break;
               },
           }
       }
    }
why this is not working ?
i am trying out rust since so long, why the hell i am not able to pick it up guys, need some trick.
Andrey Lesnikov
@ozkriff
May 01 2017 09:07
can you reproduce the error on https://play.rust-lang.org ?
VJ
@00imvj00
May 01 2017 09:08
use std::mem;

#[derive(Debug)]
struct LinkedList {
    head: Link
}

impl LinkedList {
    fn new() -> LinkedList {
        LinkedList{
            head: Link::Empty
        }
    }

    fn add(&mut self, arg: i32) {
        let new_node = Box::new(Node{
            value: arg,
            next : mem::replace(&mut self.head, Link::Empty),
        });
        self.head = Link::More(new_node);
    }

    // add new node after given node.
    fn add_after(&mut self,node: &mut Box<Node>, arg: i32) {

       let new_node = Box::new(Node{
          value: arg,
          next : mem::replace(&mut node.next, Link::Empty),
       });
       node.next = Link::More(new_node);
    }

    // add node at the end of the list.
    fn append(&mut self, arg: i32) {
       let mut current_link: &mut Link = &mut self.head; 
       loop {
           match *current_link {
                  Link::More(ref mut node) => {
                     if LinkedList::is_empty(&node) == true { //next node is empty
                        println!("Next node is empty");  
                     }else{
                        current_link = &mut node.next;
                     }
                  },
                  Link::Empty => {
                    println!("Empty got, breaking");
                    break;
               },
           }
       }
    }

    fn is_empty(node: &Node) -> bool {
        match node.next {
              Link::Empty   => true,
              Link::More(_) => false,
        }
    }


    fn traverse(&self) {
       let mut current_node = &self.head;
       while let &Link::More(ref boxed_node) = current_node {
          println!("Value : {:?}", boxed_node.value);
          current_node = &boxed_node.next;   
       }
    }

    fn remove(&mut self) -> i32 {
       let head  = mem::replace(&mut self.head, Link::Empty);
       match head {
           Link::More(boxed_val) => {
               let node  = *boxed_val;
               self.head = node.next;
               node.value
           },
           Link::Empty => {
               println!("Empty Linked list.");
               0
           },
       }
    }
}

impl Drop for LinkedList {
    fn drop(&mut self) {
       let mut cur_node = mem::replace(&mut self.head,Link::Empty);
       while let Link::More(mut boxed_node) = cur_node {
            cur_node = mem::replace(&mut boxed_node.next, Link::Empty);
        }
    }
}

#[derive(Debug)]
enum Link {
    Empty,
    More(Box<Node>),
}

#[derive(Debug)]
struct Node {
    value: i32,
    next: Link 
}

fn main() {
    let mut linked_list = LinkedList::new();

    for i in 0..100 {
        linked_list.add(i);
    }

    linked_list.traverse();
    linked_list.append(5);
}
full code.
error on rust play ground
Andrey Lesnikov
@ozkriff
May 01 2017 09:10
it's better to post here only links, like https://play.rust-lang.org/?gist=030d0e81a410c5a2f093e96b60ca343d
VJ
@00imvj00
May 01 2017 09:10
rustc 1.17.0 (56124baa9 2017-04-24)
error[E0499]: cannot borrow `current_link.0` as mutable more than once at a time
  --> <anon>:38:30
   |
38 |                   Link::More(ref mut node) => {
   |                              ^^^^^^^^^^^^
   |                              |
   |                              second mutable borrow occurs here
   |                              first mutable borrow occurs here
...
51 |     }
   |     - first borrow ends here

error[E0506]: cannot assign to `current_link` because it is borrowed
  --> <anon>:42:25
   |
38 |                   Link::More(ref mut node) => {
   |                              ------------ borrow of `current_link` occurs here
...
42 |                         current_link = &mut node.next;
   |                         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ assignment to borrowed `current_link` occurs here

error: aborting due to 2 previous errors
ok got it.
sorry for that.
Andrey Lesnikov
@ozkriff
May 01 2017 09:11
the problem is that you are making a mutable borrow in a loop
VJ
@00imvj00
May 01 2017 09:12
then what is the alternative solution ? @ozkriff
i can do it as recursive function ? @ozkriff
VJ
@00imvj00
May 01 2017 09:27
i did it.
using recursion.
VJ
@00imvj00
May 01 2017 09:34
Recursion is biggest problem in rustlang.
Frank McSherry
@frankmcsherry
May 01 2017 09:46
In case it is helpful, here is a doubly-linked list I wrote a while back. It's pretty concise. https://is.gd/bFCpMq
You don't need recursion or anything for it (though, I think you probably need unsafe to construct an empty instance of it).
Mike Lubinets
@mersinvald
May 01 2017 11:23
@maralla actually any inferior mutability container is applicable in your case, so you can use Rc<Cell> with no sync overhead at all as long as you use it in a single thread
Sathya Narrayanan
@sourcepirate
May 01 2017 15:51
struct Empty;
struct Null;

// A trait generic over `T`.
trait DoubleDrop<T> {
    // Define a method on the caller type which takes an
    // additional single parameter `T` and does nothing with it.
    fn double_drop(self, _: T);
}

// Implement `DoubleDrop<T>` for any generic parameter `T` and
// caller `U`.
impl<T, U> DoubleDrop<T> for U {
    // This method takes ownership of both passed arguments,
    // deallocating both.
    fn double_drop(self, _: T) {}
}
How could i use U Caller memebers inside double_drop function
Ingvar Stepanyan
@RReverser
May 01 2017 16:35
you can't because it's generic. you need to have exact type to access members
Jacob Payne
@Latrasis
May 01 2017 17:58
Does anybody know how BigEndian/LittleEndian encoding is dealt with for std::hash:: Hasher's? I'm noticing most hashing crates don't bind with the std::hash module.
Denis Lisov
@tanriol
May 01 2017 18:05
@Latrasis IIUC, it hashes the in-memory representation, whatever that one is.
Jacob Payne
@Latrasis
May 01 2017 18:06
How do we know if the encoding is machine independent then?
Denis Lisov
@tanriol
May 01 2017 18:08
Most likely it's not for anything that depends on endianness.
Byte arrays should be safe, however.
Jacob Payne
@Latrasis
May 01 2017 18:10
Feels like std::hash is vestigial then. Since almost no crates integrate with it.
Denis Lisov
@tanriol
May 01 2017 18:14
Hmm? Well, some parts of serde_json do use it, for example :-)
Jacob Payne
@Latrasis
May 01 2017 18:19
What i mean is if you take a look at the top crypto crates:rust-crypto, tiny-keccak, ring. Almost all of them do not bind with std::hash. Which seems like it kind of defeats the purpose of std::hash. Is there a reason none of these libraries are binding to it? There must be something missing.
Denis Lisov
@tanriol
May 01 2017 18:26
I don't know the exact reason. Maybe the maintainers will tell you in the issues you've opened. As for "the purpose of", right now the primary purpose of std::hash seems to be providing the basis for HashMap/HashSet and possibly other primitives like them.
Sergey Noskov
@Albibek
May 01 2017 18:59
usual hashes, like SipHash/FnvHash etc are different from cryptographic hashes, they have different goals and requirements, this seems to be good explanation
lberserq
@lberserq
May 01 2017 22:37
Hi guys, can anyone help, Can I built static library without rust runtime with stable channel compiler. I have tried to set panic="abort", and still getting an unresolved symbol about panic. Does anybody know how to override panic_fmt in the stable channel rust compiler, without #[lang=panic_fmt] ?
Denis Lisov
@tanriol
May 01 2017 22:38
@lberserq Rust stdlib is pretty minimal, are you sure you need to get rid of it?
lberserq
@lberserq
May 01 2017 22:45
@tanriol Ok, could you explain me where i should looking for static stdlib library file or maybe a shared lib file, because i have to use gcc as final app linker. Because currently when i am trying to built a staticlib build with cargo, i `ve got many unresolved functions in library.
Denis Lisov
@tanriol
May 01 2017 22:56
@lberserq Are you sure the unresolved functions are parts of the Rust std and not other C libraries your lib and/or its dependencies require?
lberserq
@lberserq
May 01 2017 23:05
@tanriol yes, because these functions have mangled names, e.g _ZN4core9panicking5panic17h19c36fe39589c59eE
Denis Lisov
@tanriol
May 01 2017 23:10
Hm, looks strange. You're building the library as a staticlib type crate, aren't you?
lberserq
@lberserq
May 01 2017 23:12
@tanriol yes
lberserq
@lberserq
May 01 2017 23:21
@tanriol config -- https://gist.github.com/lberserq/c9fb55a090c8c97b54e0804cf9fa0d60 src -- https://gist.github.com/lberserq/70df16f9df0dc796d990f41a61ffa88b, nm libnostd.rlib | grep "U" _ZN4core9panicking5panic17h19c36fe39589c59eE
Denis Lisov
@tanriol
May 01 2017 23:26
Your crate-type is not working, because it's expected in the [lib] section, not [package]
lberserq
@lberserq
May 01 2017 23:32
@tanriol thank you