These are chat archives for rust-lang/rust

5th
Jan 2018
Moggers
@Moggers
Jan 05 2018 01:39
Can I use servo or a subset of servo to make a HTML gui for my videogame? I did some quick poking around and it doesn't seem like people are suggesting or recommending this as a good idea
razuit
@razuit
Jan 05 2018 12:29
what is your editor (IDE?) choice for developing with Rust? anyone using VSCode?
Steve Klabnik
@steveklabnik
Jan 05 2018 13:03
i personally use VSCode
razuit
@razuit
Jan 05 2018 13:03
@steveklabnik any extensions that you recommend?
Steve Klabnik
@steveklabnik
Jan 05 2018 13:03
i use rust.rls or whatever, the one provided by the team
and Native Debug, though i rarely actual debug things
razuit
@razuit
Jan 05 2018 13:15
@steveklabnik thanks!
Steve Klabnik
@steveklabnik
Jan 05 2018 13:16
any time :)
Wouter De Coster
@wdecoster
Jan 05 2018 13:58
I recently started learning Rust and I'm a bit surprised to see a different synthax for slicing a tuple (x.0) and an array (a[0]). Can someone explain the philosophy behind this?
Steve Klabnik
@steveklabnik
Jan 05 2018 13:59
one aspect is homogeniety vs hetergeniality
so like, in order to type check
you need tuples to be accessed with syntax
you can't do it at run time
whereas an array (and vector) are all made of one type, T
so you can always check array access's types correctly, no matter what the index is
does that make sense?
Wouter De Coster
@wdecoster
Jan 05 2018 14:02
hmm I'm not sure :D
Steve Klabnik
@steveklabnik
Jan 05 2018 14:02
imagine we could use [] to index a tuple
index is a variable
tuple is a variable of type (i32, String, f32)
what type is tuple[index]?
contrast that with
vec is a variable of type Vec<i32>.
what type is vec[index]?
Wouter De Coster
@wdecoster
Jan 05 2018 14:05
So, tup.index doesn't work because it does not take a variable index but only an "exact" 0?
Steve Klabnik
@steveklabnik
Jan 05 2018 14:05
it does work due to that
it has to be a number literal
there's another aspect of it that's nice
and that's
if tuple is a variable of type (i32, String, f32), and you have tuple.5, we can say at compile time "hey that's out of bounds"
Wouter De Coster
@wdecoster
Jan 05 2018 14:06
uhu
Steve Klabnik
@steveklabnik
Jan 05 2018 14:06
in theory, we could make it panic, like vectors and []
but it's nice to have it at compile time
Wouter De Coster
@wdecoster
Jan 05 2018 14:07
alright I think I'm grasping the reasoning behind this. Thanks! Back to reading on 5. Using Structs to Structure Related Data
Steve Klabnik
@steveklabnik
Jan 05 2018 14:07
:)
enjoy
Wouter De Coster
@wdecoster
Jan 05 2018 16:24
is there a python synonym for a Rust trait?
it's something of a more advanced chapter but I find it a bit obscure so far when it's mentioned in the earlier chapters of the book
Steve Klabnik
@steveklabnik
Jan 05 2018 16:24
im not a python expert, but i don't believe so
"compile time duck typing" is like, a bad description but the closest i can think of
Wouter De Coster
@wdecoster
Jan 05 2018 16:25
Alright then I'll wait a bit until I get to those chapters. It's quite helpful to figure out what the corresponding thing is in your native language.
Steve Klabnik
@steveklabnik
Jan 05 2018 16:26
totally :)
Wouter De Coster
@wdecoster
Jan 05 2018 16:26
Structs are nice so far.
Wouter De Coster
@wdecoster
Jan 05 2018 16:38
feels very python-like
Ed Page
@epage
Jan 05 2018 16:39
In Python, they call it a Protocol or an ABC
Protocol describes the duck-typed interface. An ABC codifies a Protocol into a class you can inherit, do tests on, etc
Sherab Giovannini
@Shaddy
Jan 05 2018 17:17
hi guys, is there a "pattern" to create iterators?
for me it feels like if I create an struct that has the context of the iteration and also the proper item it is not well sepparated
Tom Cumming
@tomcumming
Jan 05 2018 17:19
@Shaddy sounds right to me
Sherab Giovannini
@Shaddy
Jan 05 2018 17:19
i've been looking at IntoIter traits and so on, maybe its the solution but just asking if there are better examples than rustbyexample/rustbook
sounds good?
so having an struct with something like, lets say
Steve Klabnik
@steveklabnik
Jan 05 2018 17:19
@Shaddy there's two traits, Iterator and IntoIterator
Sherab Giovannini
@Shaddy
Jan 05 2018 17:20
struct WillBeIterator {
     curr: usize,
     (item)name: String,
     (item)value: whatever,
     limit: usize,
     container: items
}
Steve Klabnik
@steveklabnik
Jan 05 2018 17:20
usually something that implements Iterator will have the context of the iteration and also what it's iterating over
Sherab Giovannini
@Shaddy
Jan 05 2018 17:20
for me it feels weird to share in the same struct the iteration context and the resulting item value
and i'm pretty sure im doing bad
Steve Klabnik
@steveklabnik
Jan 05 2018 17:21
what is (item)name?
Sherab Giovannini
@Shaddy
Jan 05 2018 17:21
an item name
uhm, hold a second
maybe im doing wrong question
its ok, I was just confused
Sherab Giovannini
@Shaddy
Jan 05 2018 17:26
seems that the struct that holds the context doesn't require to hold the item state, you can just return different items
sorry for the stupid question
Steve Klabnik
@steveklabnik
Jan 05 2018 17:35
no worries at all!
NOP0
@NOP0
Jan 05 2018 17:47
With GAT, can you implement monads, applicative functors etc?
Steve Klabnik
@steveklabnik
Jan 05 2018 18:37
rust doesn't have HKT
so you can't really write monads
NOP0
@NOP0
Jan 05 2018 18:40
Ok, I just read that GAT was a step towards HKT? I'll read the RFC. Thanks Steve :smile:
Steve Klabnik
@steveklabnik
Jan 05 2018 18:40
a step toward doesn't mean you've arrived yet :)
NOP0
@NOP0
Jan 05 2018 18:44
Interesting, is there a plan for arriving at "full" HKT in the end?
Steve Klabnik
@steveklabnik
Jan 05 2018 18:47
we're adding associated type constructors
those give equivalent power but make more sense right now
Restioson
@Restioson
Jan 05 2018 19:00
Will we ever have intersection types rawly
As in String | i32 being its own type rather than Foo = String(String) | Int(i32) as an enum
Steve Klabnik
@steveklabnik
Jan 05 2018 19:01
i'm not aware of any plans
Restioson
@Restioson
Jan 05 2018 19:02
i heard scala was gonna get em
I'm not sure how youd be supposed to use them
if let String = foo {
    println!("{}", foo); // foo is now known to be String
}

let x = () | i32 = match foo {
    String => println!("{}", foo),
    i32 => foo + 2, 
}
?
Perhaps they could be implemented as anonymous enums or something
With a special case such that A | B is always the same type as A | B, never a slightly different generated type
NOP0
@NOP0
Jan 05 2018 19:11
@steveklabnik thanks, will look into associated types then. cool
matrixbot
@matrixbot
Jan 05 2018 19:11
banjofox hi everyone!
Sgeo
@Sgeo
Jan 05 2018 19:42
I wrote some code for anonymous sum types like that, although the ergonomics might not be that great.
Should I put something into a crate?
Michal 'vorner' Vaner
@vorner
Jan 05 2018 19:59
@Sgeo How do you do them? Macros?
Sgeo
@Sgeo
Jan 05 2018 20:07
Macros could make it more ergonomic, but mainly by using type inference
Michal 'vorner' Vaner
@vorner
Jan 05 2018 20:08
Thanks, I'll have a look. It could be a nice read.
Sherab Giovannini
@Shaddy
Jan 05 2018 21:40
are there facilities to compare slices/vecs as subsets of other slices/vecs?
Steve Klabnik
@steveklabnik
Jan 05 2018 21:40
== works on slices
at least
Sherab Giovannini
@Shaddy
Jan 05 2018 21:42
yup, but I want to compare a subset of a vector
meaning something like [0, 1, 2, 3, 4].contains([2, 3])
what comes to my mind is to do a window as big as subset size and iterate over the slice matching the subset
Steve Klabnik
@steveklabnik
Jan 05 2018 21:43
ah
Sherab Giovannini
@Shaddy
Jan 05 2018 21:43
but I'm pretty sure that should be a facility
im not the only one to achieve this xD
Michal 'vorner' Vaner
@vorner
Jan 05 2018 21:44
Don't iterators know how to compare? If not by default, the extentions in itertools could.
Sherab Giovannini
@Shaddy
Jan 05 2018 21:45
im not sure how to answer this :/
ill take a look on itertools ty
fn eq<I>(self, other: I) -> bool 
where
    I: IntoIterator,
    Self::Item: PartialEq<I::Item>,
    Self: Sized, 
1.5.0[−]
Determines if the elements of this Iterator are equal to those of another.
could be
Sherab Giovannini
@Shaddy
Jan 05 2018 21:55
you can do this Iterator::eq(container.iter(), subset.iter())or this itertools::equal(&container, &subset) being both vectors/iterators
but doesn't seem to be a "contains" or such like this
Naja Melan
@najamelan
Jan 05 2018 22:02
is anyone working on compiling rust programs for unikraft? http://unikraft.neclab.eu/intro.html
Sherab Giovannini
@Shaddy
Jan 05 2018 22:03
this is what I had to do... if anyone has something better would be welcome
fn contains(left: &[u8], right: &[u8]) -> bool {
    (0..left.len())
        .any(|start| {
            Iterator::eq(left[start..start + right.len()].iter(), right.iter())
        })
}