These are chat archives for rust-lang/rust

29th
Jan 2018
Kelly Thomas Kline
@kellytk
Jan 29 2018 03:08
Why is "input" required twice in "use console::input::input;" on line 4 of https://play.integer32.com/?gist=d51c4d7ab02b404e5793d8cd04995675&version=stable?
Alexandre Bury
@gyscos
Jan 29 2018 05:03
In input.rs, you shouldn't write pub mod input { ... }
Otherwise, it creates an input module in the ::mod::input one
Kelly Thomas Kline
@kellytk
Jan 29 2018 05:03
What should I write?
Alexandre Bury
@gyscos
Jan 29 2018 05:03
Just directly the content of the module
It's as if the content of input.rs was inserted into a pub mod input { ... } in console.rs
which itself would be inserted in a mod console { ... } in main.rs
So similarly, you don't need pub mod console { } in console.rs
Kelly Thomas Kline
@kellytk
Jan 29 2018 05:06
Oh, thanks @gyscos
Kelly Thomas Kline
@kellytk
Jan 29 2018 10:04
Using is_ascii_alphanumeric() on a String instance generates the error "error: use of unstable library feature 'ascii_ctype'". I skimmed the associated issue but I didn't find what needs to be done so that I may make use of the feature. Can someone share a pointer?
red75prime
@red75prime
Jan 29 2018 10:09
Add #![feature(ascii_ctype)] to the source and switch to a nightly compiler branch: rustup default nightly
Kelly Thomas Kline
@kellytk
Jan 29 2018 10:57
Thanks @red75prime. Out of curiosity, why are the character classification functions oriented around ascii rather than utf-8?
red75prime
@red75prime
Jan 29 2018 11:28
There are unicode functions char::is_alphanumeric() and others. ASCII versions are probably provided to decrease the size of the executable.
Kelly Thomas Kline
@kellytk
Jan 29 2018 11:36
@red75prime Is it correct that that requires evaluating each character in a str for is_alphanumeric()?
red75prime
@red75prime
Jan 29 2018 11:44
@kellytk Not necessarily. If you know starting position of utf-8 code point, you can do something like string[pos..].chars().next().unwrap().is_alphanumeric()
utf-8 isn't very random access friendly.
Kelly Thomas Kline
@kellytk
Jan 29 2018 11:45
@red75prime What I mean is, in order to verify an entire str is_alphanumeric(), one must check that each char of the str is_alphanumeric()?
Denis Lisov
@tanriol
Jan 29 2018 14:12
Something like str.chars().all(char::is_alphanumeric) should work
Kelly Thomas Kline
@kellytk
Jan 29 2018 14:20
Thanks @tanriol I'll look up all()
Krishna Kumar T
@KrishnaKumarT36_twitter
Jan 29 2018 15:59
Hi rustaceans, I am trying to run rust backend on an Android device. It basically uses sodiumoxide that depends on libsodium library. When I run it on Mac, it perfectly works, but if I am trying to build it to android targets, it fails with linking error. I am trying to ask about it in multiple places on IRC but no one responded. Can someone help me with it?
Denis Lisov
@tanriol
Jan 29 2018 15:59
@KrishnaKumarT36_twitter What's the exact linking error?
Krishna Kumar T
@KrishnaKumarT36_twitter
Jan 29 2018 16:00
One moment, I will paste it here. @tanriol
= note: /Users/krishnak/Work/Leizure/greetings/NDK/arm/bin/../lib/gcc/arm-linux-androideabi/4.9.x/../../../../arm-linux-androideabi/bin/ld: error: sodium: no archive symbol table (run ranlib) /Users/krishnak/Work/Leizure/greetings/NDK/arm/bin/../lib/gcc/arm-linux-androideabi/4.9.x/../../../../arm-linux-androideabi/bin/ld: error: cannot find -lsqlite3 clang50: error: linker command failed with exit code 1 (use -v to see invocation)
Here is the command, I ran cargo build --target armv7-linux-androideabi --release @tanriol
Denis Lisov
@tanriol
Jan 29 2018 16:14
Looks like two separate errors...
Krishna Kumar T
@KrishnaKumarT36_twitter
Jan 29 2018 16:14
Yes...
I can remove dependency for the second one by using android's sqlite DB instead of rust's, But I dont undestand how I can get around libsodium dependency
Denis Lisov
@tanriol
Jan 29 2018 16:15
Do you have ar set in .cargo/config alongside with the linker?
Krishna Kumar T
@KrishnaKumarT36_twitter
Jan 29 2018 16:16
yes..I do..
ar = "/Users/krishnak/Work/Leizure/greetings/NDK/arm/bin/arm-linux-androideabi-ar"
linker = "/Users/krishnak/Work/Leizure/greetings/NDK/arm/bin/arm-linux-androideabi-clang"
This is how it looks like in my config file
Jordano Moscoso
@PlayLights_twitter
Jan 29 2018 16:22
Has anybody tried to send a pdf file to a printer in rust-lang?
Maciej Gorywoda
@makingthematrix
Jan 29 2018 20:59
Hi, guys.
This will be a long question, and I understand it's quite the beginner level, but please bear with me. I'm quite okay with finding solving it by myself, but I'm looking for a more idiomatic way to do it.

I have a data structure made from a Vec<usize> (it's a set of usizes, but it's not important). For this USet I implemented the Sub trait:

```
impl Sub for USet {
    type Output = USet;
    fn sub(self, other: USet) -> USet { ... }
}
```

to be able to do this:

```
let s1 = USet::from(vec![0, 3, 8, 10]);
let s2 = USet::from(vec![3, 8]);
let s3 = s1 - s2;
```

So far so good, but: USet by design does not implement Copy. When I call a method taking a USet I want to use the original one, not a copy. This is mainly for performance, so if I find a good explanation that the compiler will magically compile the copy away, I might be persuaded to implement Copy and live happily ever after. But let's assume that I really don't want to do it. It means that, as far as I understand, when I call sub I transfer the ownership of s1 and s2. So, the above example works, but this one does not:

    let s1 = USet::from(vec![0, 3, 8, 10]);
    let s2 = USet::from(vec![3, 8]);

    for _i in 1..10 {
       let s3 = s1 - s2;
    }

error[E0382]: use of moved value: `s1`
  --> src/utils/uset_tests.rs:84:28
   |
84 |             let s3 = s1 - s2;
   |                      ^^ value moved here in previous iteration of loop
   |
   = note: move occurs because `s1` has type `utils::uset::USet`, which does not implement the `Copy` trait

I know there is this concept of "borrowing". In fact, for old people like me it's pretty easy
to understand - I learnt C/C++ when it was still da thing - so I quickly wrote this:

    impl USet {
        pub fn substract(&self, other: &USet) -> USet { ... }
    }
    ...
    for _i in 1..10 {
        let s3 = s1.substract(&s2);
    } // tadaaah!

but it's not really what I want. I want to write clear, easy to read and understand operations on sets.
I want s1 * (s2 + s3) - s4, not s1.multiply(&(s2.add(&s3))).substract(&s4).
I thought that maybe it was possible to implement Sub on references, even if it sounds weird:

    impl<'a> Sub for &'a USet {
        type Output = USet;
        fn sub(&self, other: &'a USet) -> USet { self.substract(other) }
    }

but no:

error[E0053]: method `sub` has an incompatible type for trait
   --> src/utils/uset.rs:146:5
    |
146 |     fn sub(&self, other: &'a USet) -> USet {self.substract(other) }
    |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected struct `utils::uset::USet`, found reference
    |
    = note: expected type `fn(&'a utils::uset::USet, &'a utils::uset::USet) -> utils::uset::USet`
               found type `fn(&&'a utils::uset::USet, &'a utils::uset::USet) -> utils::uset::USet`

because self here is already a reference, so &self is a reference to a reference. But if I change the declaration to fn sub(self, other &'a USet) I'm back to square one: use of moved value s1 in the loop.

Any ideas? Could you at least point me to some articles about this stuff?

Denis Lisov
@tanriol
Jan 29 2018 21:06
Do you mean something like this?
Roman Proskuryakov
@kpp
Jan 29 2018 21:46
@makingthematrix you should implement fn sub(self, ...) because math ops accept self by value
And in your case you implement Sub for &'a USet which means that in fn sub(self ...) self will be a &'a USet
Maciej Gorywoda
@makingthematrix
Jan 29 2018 22:09
oh. thanks :) I'll try that tomorrow and let you know :)