These are chat archives for rust-lang/rust

18th
May 2017
Fra ns
@snarf95_twitter
May 18 2017 08:16
pub fn from(value: u32) -> Self {
    let mut result: Vec<String> = Vec::new();
    let mut value = value.clone();
    let literals = [['I', 'V', 'X', ' '],
                    ['X', 'L', 'C', ' '],
                    ['C', 'D', 'M', ' '],
                    ['M', ' ', ' ', ' ']];
    let mut a = 0;
    loop {
        result.push(match value % 10 {
                            1 => [0, 3, 3, 3],
                            2 => [0, 0, 3, 3],
                            3 => [0, 0, 0, 3],
                            4 => [0, 1, 3, 3],
                            5 => [1, 3, 3, 3],
                            6 => [1, 0, 3, 3],
                            7 => [1, 0, 0, 3],
                            8 => [1, 0, 0, 0],
                            9 => [0, 2, 3, 3],
                            _ => [3, 3, 3, 3],
                        }
                        .iter()
                        .map(|&i| literals[a][i as usize])
                        .filter(|&c| !c.is_whitespace())
                        .collect::<String>());

        value /= 10;
        a += 1;
        if value == 0 {
            break;
        }
    }

    result.reverse();
    Roman(result.join(""))
}
In this case, why am I not allowed to have slices of different length in match arms?
Zakarum
@omni-viral
May 18 2017 08:17
Cause they are not slices
Fra ns
@snarf95_twitter
May 18 2017 08:17
it's fixed arrays, yeah but still?
Zakarum
@omni-viral
May 18 2017 08:17
Fixed arrays have size as part of a type
Fra ns
@snarf95_twitter
May 18 2017 08:18
I don't see why it should matter that they have different length in this case since they're consumed by iter anyway
Jonas Platte
@jplatte
May 18 2017 08:18
Your match has to have exactly one type
It doesn't matter how its result is used
Fra ns
@snarf95_twitter
May 18 2017 08:19
why does it work when returning &str then?
Zakarum
@omni-viral
May 18 2017 08:19
Cause &str does not have size as part of a type
Same as &[usize] would work
Actually you can just add & before you arrays to convert them to slices
And then they can have any size you want
Fra ns
@snarf95_twitter
May 18 2017 08:20
yeah but then i'm getting values dropped :/
Jonas Platte
@jplatte
May 18 2017 08:21
dropped? What exactly are you talking about?
Fra ns
@snarf95_twitter
May 18 2017 08:21
borrowed value does not live long enough
Jonas Platte
@jplatte
May 18 2017 08:22
Well this seems to almost be a self-contained example, so I can try to reproduce it.
Fra ns
@snarf95_twitter
May 18 2017 08:23
error[E0308]: match arms have incompatible types
when doing:
match value % 10 {
                            1 => &[0],
                            2 => &[0, 0],
                            3 => &[0, 0, 0],
                            4 => &[0, 1],
                            5 => &[1],
                            6 => &[1, 0],
                            7 => &[1, 0, 0],
                            8 => &[1, 0, 0, 0],
                            9 => &[0, 2],
                            _ => &[],
                        }
Jonas Platte
@jplatte
May 18 2017 08:25
Yeah, deref coercion doesn't happen for match it seems
You don't actually convert to a slice with &, you are actually just taking a reference (so &[0u32] is a &[u32;1])
Zakarum
@omni-viral
May 18 2017 08:26
You can force deref coercion
Jonas Platte
@jplatte
May 18 2017 08:26
but &[u32;1] can be coerced into &[u32] implicitly
Zakarum
@omni-viral
May 18 2017 08:26
just make it let x: &[u32] = match …
Jonas Platte
@jplatte
May 18 2017 08:26
@SCareAngel Or just make the slicing explicit with [0][..] instead.
In any case the lifetime of these arrays is still an issue
Actually with [..] I just got an error message I never saw before :D
error[E0507]: cannot move out of indexed content
Fra ns
@snarf95_twitter
May 18 2017 08:29
error: borrowed value does not live long enough
when doing let x: &[u32] = match
error: borrowed value does not live long enough
  --> src\lib.rs:16:37
   |
16 |                             1 => &[0],
   |                                   --^
   |                                   | |
   |                                   | temporary value dropped here while still borrowed
   |                                   temporary value created here
...
38 |     }
   |     - temporary value needs to live until here
Jonas Platte
@jplatte
May 18 2017 08:30
I'm actually slightly confused as to why these arrays are not static data instead of temporary values..
Zakarum
@omni-viral
May 18 2017 08:30
Provide me with rest of the code pls
Fra ns
@snarf95_twitter
May 18 2017 08:30
impl Roman {
    pub fn from(value: u32) -> Self {
        let mut result: Vec<String> = Vec::new();
        let mut value = value.clone();
        let literals = [['I', 'V', 'X'],
                        ['X', 'L', 'C'],
                        ['C', 'D', 'M'],
                        ['M', ' ', ' ']];
        let mut a = 0;
        loop {
            let b: &[u32] = match value % 10 {
                1 => &[0],
                2 => &[0, 0],
                3 => &[0, 0, 0],
                4 => &[0, 1],
                5 => &[1],
                6 => &[1, 0],
                7 => &[1, 0, 0],
                8 => &[1, 0, 0, 0],
                9 => &[0, 2],
                _ => &[],
            };
            result.push(b.iter()
                            .map(|&i| literals[a][i as usize])
                            .filter(|&c| !c.is_whitespace())
                            .collect::<String>());

            value /= 10;
            a += 1;
            if value == 0 {
                break;
            }
        }

        result.reverse();
        Roman(result.join(""))
    }
}
Zakarum
@omni-viral
May 18 2017 08:31
And definition of Roman pls )
Fra ns
@snarf95_twitter
May 18 2017 08:31
pub struct Roman(String);
Jonas Platte
@jplatte
May 18 2017 08:32
@snarf95_twitter Please post long code examples to play.rust-lang.org or play.integer32.com instead of pasting them into the chat in the future.
Then you can also make sure that we can see the same error message you do.
Fra ns
@snarf95_twitter
May 18 2017 08:33
@jplatte will do
it works
Fra ns
@snarf95_twitter
May 18 2017 08:36
heh wonder why match won't work tho
Zakarum
@omni-viral
May 18 2017 08:36
It can’t work with match because match have {}
And everything created inside {} will be dropped outside
unless moved
Fra ns
@snarf95_twitter
May 18 2017 08:37
hmm
Zakarum
@omni-viral
May 18 2017 08:37
This is useful though. You can borrow inside match and borrowing will end outside
Fra ns
@snarf95_twitter
May 18 2017 08:38
thanks for help
Zakarum
@omni-viral
May 18 2017 08:41
alt
Alyani
@notsonotso
May 18 2017 08:55
today I hate the borrow checker
tomorrow, who knows
Sherzod Mutalov
@shmutalov
May 18 2017 09:00
@notsonotso borrow checker is cute >(^.^)<
Alyani
@notsonotso
May 18 2017 09:02
and sometimes a biaaaatch
Fra ns
@snarf95_twitter
May 18 2017 09:04
would like to hear your opinion on readability/performance etc
sadly couldn't get rev working on final vec... so I need to do let mut ...
Jonas Platte
@jplatte
May 18 2017 09:08
@snarf95_twitter You should be able to reverse before collecting with https://doc.rust-lang.org/std/iter/trait.Iterator.html#method.rev
Maybe you tried reverse instead of rev because that's what it's called for Vec / String?
Fra ns
@snarf95_twitter
May 18 2017 09:11
Might be the reason yeah
Fra ns
@snarf95_twitter
May 18 2017 09:38
actually I get error[E0277]: the trait bound `std::str::Chars<'_>: std::iter::ExactSizeIterator` is not satisfied
with rev
Jonas Platte
@jplatte
May 18 2017 09:39
Oh, rev requires ExactSizeIterator?
That's interesting
Zakarum
@omni-viral
May 18 2017 09:39
That my version https://is.gd/vl8T9o
Jonas Platte
@jplatte
May 18 2017 09:40
Actually, it requires DoubleEndedIterator...
Fra ns
@snarf95_twitter
May 18 2017 09:40
@SCareAngel that's pretty cool tbh
Zakarum
@omni-viral
May 18 2017 09:41
https://is.gd/LDEk5S
Remove redundant collect::<Vec<_>>()
Jonas Platte
@jplatte
May 18 2017 09:42
@SCareAngel You should use more whitespace though ^^
Zakarum
@omni-viral
May 18 2017 09:42
shit. Some tests are failed now ((
Jonas Platte
@jplatte
May 18 2017 09:42
The lines in your code are very long, and there isn't even a blank line between the two impl blocks
the other playground can run rustfmt for you :)
Zakarum
@omni-viral
May 18 2017 09:44
@jplatte whitespaces?? what is it??
Fra ns
@snarf95_twitter
May 18 2017 09:48
Jonas Platte
@jplatte
May 18 2017 09:49
@snarf95_twitter you broke it though
you removed significant whitespace
Fra ns
@snarf95_twitter
May 18 2017 09:49
I'm newb
Jonas Platte
@jplatte
May 18 2017 09:50
put some more effort into your shitposts / -comments! :D
Zakarum
@omni-viral
May 18 2017 09:51
Gotta go. If I wouldn't parse those logs. No one will
Fra ns
@snarf95_twitter
May 18 2017 09:59
Back to wpf for me ;8
♠ COCO DE VIENNE ♠
@cocodevienne
May 18 2017 13:57
Quick question people: Is there a way to write and run Rust on Android?
Ilya Bogdanov
@vitvakatu
May 18 2017 13:58
@cocodevienne yes :)
Ian Johnson
@yetea
May 18 2017 13:59
I was looking into that a bit ago. I haven’t tried it yet, but I did find these: https://github.com/tomaka/android-rs-glue https://github.com/Geal/rust_on_mobile
Ilya Bogdanov
@vitvakatu
May 18 2017 14:00
I know that android-rs-glue works well, even for games (with gfx-rs)
Andrey Lesnikov
@ozkriff
May 18 2017 19:20
latest gfx-rs on android is broken right now - gfx-rs/gfx#1267 . But gfx-rs 0.12 works fine :)
Fra ns
@snarf95_twitter
May 18 2017 20:01
is it possible to reduce collect -> rev -> collect in this case? https://is.gd/EaeXx8
Denis Lisov
@tanriol
May 18 2017 20:15
@snarf95_twitter Some parts of this solution seem suboptimal to me...
Fra ns
@snarf95_twitter
May 18 2017 20:21
Yeah I know but couldn't figure out a way to do it without using traditional loops. I'm not really optimizing for performance. Instead I'm trying to overuse higher order functions
Yeah can see how fold could be optimized by using a tuple and doing pow manually
Denis Lisov
@tanriol
May 18 2017 20:36
Well, for the first heap of combinators it's just
let value = number
    .iter()
    .fold(0, |acc, d| acc * from_base + d);
Fra ns
@snarf95_twitter
May 18 2017 20:44
Hah yeah :) probably should use my brain sometimes
Denis Lisov
@tanriol
May 18 2017 20:51
...and speaking about the collect-rev-collect dance... not much can be done. I'd actually have let mut digits: Vec<...> = (...).collect(); digits.reverse(); Ok(digits)
Fra ns
@snarf95_twitter
May 18 2017 20:57
Hmm okay thanks
Denis Lisov
@tanriol
May 18 2017 20:59
Well, you can actually generate the digits straight away in the correct order, but that's not exactly obvious...
Jason Ozias
@CraZySacX
May 18 2017 21:02
note: /data/projects/rust-lang/mussh/target/release/deps/mussh-51b7d0e39fd2adc7.alloc-arc.volatile.o: In function `<alloc::arc::Arc<T>>::drop_slow':
          alloc-arc.volatile.rs:(.text._ZN33_$LT$alloc..arc..Arc$LT$T$GT$$GT$9drop_slow17h1c784d181646278eE+0x4a): undefined reference to `__rust_deallocate'
          alloc-arc.volatile.rs:(.text._ZN33_$LT$alloc..arc..Arc$LT$T$GT$$GT$9drop_slow17h1c784d181646278eE+0x7c): undefined reference to `__rust_deallocate'
          alloc-arc.volatile.rs:(.text._ZN33_$LT$alloc..arc..Arc$LT$T$GT$$GT$9drop_slow17h1c784d181646278eE+0x9f): undefined reference to `__rust_deallocate'
          /data/projects/rust-lang/mussh/target/release/deps/mussh-51b7d0e39fd2adc7.alloc-arc.volatile.o: In function `<alloc::arc::Arc<T>>::drop_slow':
          alloc-arc.volatile.rs:(.text._ZN33_$LT$alloc..arc..Arc$LT$T$GT$$GT$9drop_slow17h332e22ca6a81cf2eE+0x7b): undefined reference to `__rust_deallocate'
          /data/projects/rust-lang/mussh/target/release/deps/mussh-51b7d0e39fd2adc7.alloc-arc.volatile.o: In function `<alloc::arc::Arc<T>>::drop_slow':
          alloc-arc.volatile.rs:(.text._ZN33_$LT$alloc..arc..Arc$LT$T$GT$$GT$9drop_slow17h372357bfbc7eee72E+0x7f): undefined reference to `__rust_deallocate'
Has anyone seen an error like this before? Only occurs when building on nightly in release mode.
Ashley Mannix
@KodrAus
May 18 2017 21:37
Is there a convention in Rust yet for what to call methods that return Futures, when there's also a synchronous variant? Like Request.send vs Request.send_async or send_deferred or send_paint_the_bikeshed_green?