These are chat archives for rust-lang/rust

21st
Jul 2017
Zakarum
@omni-viral
Jul 21 2017 06:08

@stevensonmt

Are you sure that works? Seems like it would go out of scope.

yes.
slice is not actual array of objects but &mut [T]. It can go out of scope. In second line I rebind part of slice to the same variable.

matanster
@matanster
Jul 21 2017 08:05
I just realized the bulk of rust support and discussion goes on in IRC
Any modern client application for that ancient chat system that you'd recommend?
David Harvey-Macaulay
@alteous
Jul 21 2017 08:07
@matanster I like HexChat.
matanster
@matanster
Jul 21 2017 08:08
thanks @alteous
boy does that feel like UI from the 80s
David Harvey-Macaulay
@alteous
Jul 21 2017 08:10
I admire its simplicity. :)
lemonxah
@lemonxah
Jul 21 2017 08:32
good day guys
ohw do you make a function like None::<i32>
Max Frai
@max-frai
Jul 21 2017 08:33
Why do you need this? :)
lemonxah
@lemonxah
Jul 21 2017 08:34
i was trying to find it in the source but i have not been successfull
i want to make a Moniod::empty<i32> that would then make an empty monoid of that type
or even mempty<i32>
Michal 'vorner' Vaner
@vorner
Jul 21 2017 08:43
That happens kind of automatically, if that function is type-parametrized. So, something like:
fn mempty<T>() -> Monoid<T> {
  unimplemented!();
}
lemonxah
@lemonxah
Jul 21 2017 08:44
ok but i have to cann that mempty<i32>()
or can i skip the ()
that is actually what i am after
Michal 'vorner' Vaner
@vorner
Jul 21 2017 08:55
Ah, that thing. Well, None is not a function. It is a variant constructor in an enum
if you have an enum, then every variant gets a constructor created
which acts as a function if the variant carries data (eg. Some(42)), but as a constant when it carries no data.
lemonxah
@lemonxah
Jul 21 2017 08:56
yes but that only makes None work
Michal 'vorner' Vaner
@vorner
Jul 21 2017 08:56
Well, enums can be type parametrised as well
enum Option<T> {
  None,
  Some(T),
}
Some(42) works, because it can guess what T is from that 42.
For None, T can be anything. So you need to hint it.
RuRu92
@RuRu92
Jul 21 2017 08:58
How to specify a life time for the account: T ? My account is a trait and I want to have a pub accounts: Arc<Mutex<Vec<Box<Account>>>>
Would that be a right way to go about this? A bit tough to model abstractions in Rust after coming from Java/Scala
 fn add_Account<T: Account>(&mut self, account: T) {
            let mut acc = account;
            let mut accounts = self.accounts.lock().unwrap();
            accounts.push(Box::new(acc));
        }
Zakarum
@omni-viral
Jul 21 2017 08:59
 fn add_Account<T: 'static + Account>(&mut self, account: T) {
            let mut acc = account;
            let mut accounts = self.accounts.lock().unwrap();
            accounts.push(Box::new(acc));
        }
like this
RuRu92
@RuRu92
Jul 21 2017 08:59
Thanks! That works :)
Another question, do I have to box trait values?
Zakarum
@omni-viral
Jul 21 2017 09:00
Traits objects are unsized
RuRu92
@RuRu92
Jul 21 2017 09:00
Why cant I just have a vector of interfaces (accounts)
What does unsized means?
Zakarum
@omni-viral
Jul 21 2017 09:01
Size of objects is unknown
Consider this
trait T {}
struct S1;
struct S2(i32);

let x: Vec<T> = vec![S1, S2(11)];
How Vec<T> can allocate memory and store objects of different size?
Box<T> have known at compile time size
which is size of a pointer
Denis Lisov
@tanriol
Jul 21 2017 09:03
@omni-viral Not exactly size of a pointer, AFAIK.
Zakarum
@omni-viral
Jul 21 2017 09:04
@tanriol whoops
two pointers
One for object itself and one for trait meta-data
RuRu92
@RuRu92
Jul 21 2017 09:04
It's because S1 and S2 take different amount of ram on the heap?
Zakarum
@omni-viral
Jul 21 2017 09:05
defferent amount of memory, yes
and because Vec stores objects continously
RuRu92
@RuRu92
Jul 21 2017 09:05
And it's not a violation of best practices to wrap Traits in boxes?
Zakarum
@omni-viral
Jul 21 2017 09:06
The best practice is to avoid trait objects when you can
Denis Lisov
@tanriol
Jul 21 2017 09:06
@RuRu92 Not a violation, if you do need to use trait objects it's the correct approach.
Zakarum
@omni-viral
Jul 21 2017 09:06
If you can't - Use either Box, Rc or something else that can hold ?Sized types
One can even create a single linked list which can hold ?Sizedtypes
Not sure why there is no such thing in std
RuRu92
@RuRu92
Jul 21 2017 09:08

Lets say we have a case where we have a abstract Account and then from that account we can have SavingsAccount and BudgetAccount. How would you model? I can only see it by using traits?

Although I would have duplicated code
impl Account for Savings... and impl Account for Budget

Denis Lisov
@tanriol
Jul 21 2017 09:08
Single-linked lists are unpretty slow... a Vec<Box<Trait>> is going to be much faster usually.
Zakarum
@omni-viral
Jul 21 2017 09:09
@RuRu92 use enum in case you can statically know all Account types
RuRu92
@RuRu92
Jul 21 2017 09:09
Hmm.. linked list aren't nice I agree
Denis Lisov
@tanriol
Jul 21 2017 09:09
@RuRu92 What's the actual difference between these? Are they all that different?
Zakarum
@omni-viral
Jul 21 2017 09:09
@tanriol not sure what would be faster Vec<Box<Trait>> or SList<Trait>. Box<Trait> implies additional memory lookup
RuRu92
@RuRu92
Jul 21 2017 09:10
Lets asume they will have special functionality for each and different interests rates for different types of accounts
Enums could be a good approach to this
Zakarum
@omni-viral
Jul 21 2017 09:11
@RuRu92 Can you make it this way?
enum AnyAccount {
    BudgetAccount(...),
    SavingsAccount(...),
   ///etc
}
RuRu92
@RuRu92
Jul 21 2017 09:11
I am new to Rust, hope I am not asking stupid questions
Zakarum
@omni-viral
Jul 21 2017 09:12
Vec<AnyAccount> will be traversed way more faster then Vec<Box<Account>>
Because cache and things
RuRu92
@RuRu92
Jul 21 2017 09:14
AnyAccount.BudgetAccount(...) can contain a concrete struct of BudgetAccount right?
Zakarum
@omni-viral
Jul 21 2017 09:14
Yeap
RuRu92
@RuRu92
Jul 21 2017 09:14
Neat.. I do like that approach
Also easier to add new types of accounts in the future
RuRu92
@RuRu92
Jul 21 2017 09:20
Are you guys using Rust in companies?
Sherab Giovannini
@Shaddy
Jul 21 2017 09:20
I'm starting to implant it @RuRu92
Denis Lisov
@tanriol
Jul 21 2017 09:21
Yeah, using it in some test setups.
Zakarum
@omni-viral
Jul 21 2017 09:21
Some companies are ruled by old-crusty men. Who prohibit any new technology
Even python 3
If you just say "Rust" you'll be fired
Sherab Giovannini
@Shaddy
Jul 21 2017 09:22
thats dumb :(
Zakarum
@omni-viral
Jul 21 2017 09:23
And sometimes it is one of the biggest IT company in country
RuRu92
@RuRu92
Jul 21 2017 09:23
Haha, that is crazy! But I do know it's hard to promote new tech in some companies. At where I am we use Java, and if you say Scala, they panic!
Sherab Giovannini
@Shaddy
Jul 21 2017 09:23
in our case we are starting to only use Rust for system developments, also there is a project which implements a windows driver (kmd-rs) that will be updated with 1.19 so we will start to also experiment implementing windows drivers with Rust
that's common in most countries @RuRu92
mostly because national administrations use people taxes money to generate more Java jobs :D
RuRu92
@RuRu92
Jul 21 2017 09:24
@Shaddy Sounds pretty cool! Although not big fan of windows, but using Rust for system development would be interesting :D
Well I have no problem with Java, I think its great and would rather do that then go to .NET and M$ world
Sherab Giovannini
@Shaddy
Jul 21 2017 09:25
well, our main-stream devs are on windows and we bet for the effort in Rust
RuRu92
@RuRu92
Jul 21 2017 09:25
JVM by it self is rather strong. But I do find Rust appealing
How does Rust run on windows, without hicups?
Sherab Giovannini
@Shaddy
Jul 21 2017 09:26
what do you mean?
RuRu92
@RuRu92
Jul 21 2017 09:28
Well some libraries or languages run different on different systems. For instance had issues running and setting up Ruby and Rails on Windows. No trouble on Linux though. Version support may also be different from OS
Sherab Giovannini
@Shaddy
Jul 21 2017 09:28
Rust compiles directly into native code, no problems so far, there are some missing stuff with allocation libs and so on, but 1.19 is promising
RuRu92
@RuRu92
Jul 21 2017 09:28
Ease of use depending on the system is also important
Sherab Giovannini
@Shaddy
Jul 21 2017 09:28
Had no problems in that regard
anyway, we are still in experimental stage, so nothing serious so far
RuRu92
@RuRu92
Jul 21 2017 09:29
That is good, it does help that it is a system language. And 1.19 does look good
@omni-viral What kind of evil company do you work for?
Sherab Giovannini
@Shaddy
Jul 21 2017 09:30
well for me, as a mainly C (sometimes C++) and for systems and Python developer Rust is the most sexy connection between those languages I found ever
as expressive as Python, as control as C, as safe (in safe mode :D) as Rust :P
lemonxah
@lemonxah
Jul 21 2017 09:31
F: Fn(&'a A) -> B what is the 'a?
RuRu92
@RuRu92
Jul 21 2017 09:31
Haha! Well I dont like Python for it's strange syntax and magic, but I do damn like Rust and C++.
Sherab Giovannini
@Shaddy
Jul 21 2017 09:31
I used to love the simplicity of Python syntax, and beauty phylosphy around there
but the main problem with Python, especially after playing with Rust, is that you have to enforce a lot your tests, to get the same productivity as the compiler Rust gives to you
since main problems are detected in compile time
RuRu92
@RuRu92
Jul 21 2017 09:33
I do know there is a large community behind Python, but never had to work with it and didnt strike as the appealing language
Exactly
I found dynamic languages to be rather evil
Sherab Giovannini
@Shaddy
Jul 21 2017 09:33
python is great because it took a huge community that never even had a programming language, and force them to think and share they ideas
RuRu92
@RuRu92
Jul 21 2017 09:34
Especially Groovy when I worked with that. It drove me mad at some points
Sherab Giovannini
@Shaddy
Jul 21 2017 09:34
those ideas also can be exported to any language
RuRu92
@RuRu92
Jul 21 2017 09:34
Yup, thats what Python and Ruby did
Sherab Giovannini
@Shaddy
Jul 21 2017 09:34
the power of those dynamic languages are, if you're in some kind of research
RuRu92
@RuRu92
Jul 21 2017 09:34
Ruby is in the same category I would say, but for some reason it's dying out
lemonxah
@lemonxah
Jul 21 2017 09:34
i dislike interpreted languages/ dynamic languages
its a big mess
Sherab Giovannini
@Shaddy
Jul 21 2017 09:34
the power of simple doing easy powerful "commands"
RuRu92
@RuRu92
Jul 21 2017 09:34
Thats what I feel as well @lemonxah
Zakarum
@omni-viral
Jul 21 2017 09:35
@RuRu92 I'll just say that it is a google competitor at local market
Sherab Giovannini
@Shaddy
Jul 21 2017 09:35
yep, for a big project may not be the best choice for sure
but if you want to prototype some concept really fast
lemonxah
@lemonxah
Jul 21 2017 09:35
if you have to use linters and write unit tests to make sure the code will not cause runtime errors .. its not a langauge that you should be using .. ever
Sherab Giovannini
@Shaddy
Jul 21 2017 09:35
or just bind some connections to a library and test it, they payoff
RuRu92
@RuRu92
Jul 21 2017 09:35
@omni-viral You are saturated in the USA?
Pretty cool though
Sherab Giovannini
@Shaddy
Jul 21 2017 09:36
thats true @lemonxah
lemonxah
@lemonxah
Jul 21 2017 09:36
@Shaddy i can prototype WAY faster in Scala / Haskell / Rust than i can in any dynamic language
Zakarum
@omni-viral
Jul 21 2017 09:36
Far away from USA
RuRu92
@RuRu92
Jul 21 2017 09:36
Europe? UK?
lemonxah
@lemonxah
Jul 21 2017 09:36
since i dont have to worry about type errors
<-- south africa
RuRu92
@RuRu92
Jul 21 2017 09:37
@lemonxah That depends on the level of experience with the language
lemonxah
@lemonxah
Jul 21 2017 09:37
so very far away
Zakarum
@omni-viral
Jul 21 2017 09:37
@lemonxah , @RuRu92 was closer )
RuRu92
@RuRu92
Jul 21 2017 09:37
Ireland or Denmark?
Zakarum
@omni-viral
Jul 21 2017 09:37
One more hint. It is a big one
Sherab Giovannini
@Shaddy
Jul 21 2017 09:37
@lemonxah yep, but I was talking about "before Rust comes up"
:D
I was not used to those functional languages
so my only tool to express my self was the functional features of python
:(
RuRu92
@RuRu92
Jul 21 2017 09:38
France or Germany. A big one, well Russia, but I would doubt that
lemonxah
@lemonxah
Jul 21 2017 09:38
@Shaddy haskell has been around for many years i still prototype in that often
Zakarum
@omni-viral
Jul 21 2017 09:38
@RuRu92 why doubt? )
Sherab Giovannini
@Shaddy
Jul 21 2017 09:38
yes I'm aware of it, but it is a language that you learn at University, and I didn't had the chance to went in
Denis Lisov
@tanriol
Jul 21 2017 09:38
@omni-viral Is Yandex really that conservative?
lemonxah
@lemonxah
Jul 21 2017 09:38
a lot of new "developers" use js and other dynamic languages and fall in love with them because of how "easy" it was to get into it
Sherab Giovannini
@Shaddy
Jul 21 2017 09:38
so, it get lost in the travel
RuRu92
@RuRu92
Jul 21 2017 09:38
I dunno, depends on the company but I wouldn't think Russia would be strong in IT
Sherab Giovannini
@Shaddy
Jul 21 2017 09:38
*got
Zakarum
@omni-viral
Jul 21 2017 09:38
@tanriol pretty much
Ilya Bogdanov
@vitvakatu
Jul 21 2017 09:39
My vision of yandex just changed :D
Sherab Giovannini
@Shaddy
Jul 21 2017 09:39
I started with basic, later asm, then C, playing at some time with crappy VisualBasic and later discovered python, around ~2000 @lemonxah
RuRu92
@RuRu92
Jul 21 2017 09:39
:D
You work for yandex?
Sherab Giovannini
@Shaddy
Jul 21 2017 09:40
in my case I used it as you use haskell probably
Zakarum
@omni-viral
Jul 21 2017 09:40
Actually everthing depends on the importance of the project
If you make something completly unimportant - you can use whatever technology you want
RuRu92
@RuRu92
Jul 21 2017 09:40
@Shaddy So you practically lived with dynamic languages from the start
Sherab Giovannini
@Shaddy
Jul 21 2017 09:41
yes :(
Zakarum
@omni-viral
Jul 21 2017 09:41
@RuRu92 yes
Sherab Giovannini
@Shaddy
Jul 21 2017 09:42
never found a pure functional language like scala or haskell
RuRu92
@RuRu92
Jul 21 2017 09:42
Nice! In Russia I take it?
Zakarum
@omni-viral
Jul 21 2017 09:42
Yeap
Sherab Giovannini
@Shaddy
Jul 21 2017 09:42
the closest was python
RuRu92
@RuRu92
Jul 21 2017 09:42
Russian your self?
lemonxah
@lemonxah
Jul 21 2017 09:42
functional is hte best
RuRu92
@RuRu92
Jul 21 2017 09:42
Try scala, I really like it
lemonxah
@lemonxah
Jul 21 2017 09:42
scala is win :P
Zakarum
@omni-viral
Jul 21 2017 09:43
@RuRu92 pretty much :smile:
RuRu92
@RuRu92
Jul 21 2017 09:43
It's awesome and with Dotty compiler coming it. It will be even more awesome
I am Russian my self :D
Sherab Giovannini
@Shaddy
Jul 21 2017 09:44
what kind of stuff can you do with functional languages?=
never give a try
Zakarum
@omni-viral
Jul 21 2017 09:44
@RuRu92 there are plenty of russians in rust community as I see )
Sherab Giovannini
@Shaddy
Jul 21 2017 09:44
xD
Rust == Russian
XDDD
RuRu92
@RuRu92
Jul 21 2017 09:44
@omni-viral Thats what I noticed too :D
Andrey Lesnikov
@ozkriff
Jul 21 2017 09:44
@omni-viral especially in gitter )
RuRu92
@RuRu92
Jul 21 2017 09:45
@Shaddy Whole thinking approach is different. I actually find it challenging to get into functional thinking, but the more I do it the more I see the power of it
it's more expressive
Sherab Giovannini
@Shaddy
Jul 21 2017 09:45
do you have any example?
just some little piece of code to share or so
Try looking around here
It's a FRM
Zakarum
@omni-viral
Jul 21 2017 09:47
@ozkriff Funny thing when a few people in gitter chat all speak in very bad english (as myself) and after a few hours they find out that they all are russians )
RuRu92
@RuRu92
Jul 21 2017 09:47
Functional Relational Mapper
for scala
@omni-viral I know right. My English shouldn't be that bad, lived in UK for 10 years ~
Denis Lisov
@tanriol
Jul 21 2017 09:47
...and still continue speaking English, of course :-)
Sherab Giovannini
@Shaddy
Jul 21 2017 09:48
which is better scala or haskell? I know its not a good question but
RuRu92
@RuRu92
Jul 21 2017 09:48
Yeah it would be rude to change to Russian when people talk in English here
Sherab Giovannini
@Shaddy
Jul 21 2017 09:48
you know, for which one will you start watching at
yeah please, don't switch to Russian xD
RuRu92
@RuRu92
Jul 21 2017 09:48
Oh boy... that can start a language war mate
Sherab Giovannini
@Shaddy
Jul 21 2017 09:48
it's even hard to express in english sometimes
Zakarum
@omni-viral
Jul 21 2017 09:49
@RuRu92 Never been in UK or USA. So all my english skills going from chatting )
Sherab Giovannini
@Shaddy
Jul 21 2017 09:49
that introduction reminds me a lot of python stuff
Denis Lisov
@tanriol
Jul 21 2017 09:49
Are you thinking with English terminology or Russian when programming in Rust? :-)
RuRu92
@RuRu92
Jul 21 2017 09:49
I learned by playing games :D
Sherab Giovannini
@Shaddy
Jul 21 2017 09:49
yielding, mapping, filtering, sorting and so on
Zakarum
@omni-viral
Jul 21 2017 09:49
@RuRu92 everyone start with games )
Sherab Giovannini
@Shaddy
Jul 21 2017 09:49
which also Rust does great
but well, Rust doesn't have the "generator" concept, isn't it?
Zakarum
@omni-viral
Jul 21 2017 09:50
@tanriol I think in Rust when programming in Rust )
@Shaddy "generator" like in python?
Sherab Giovannini
@Shaddy
Jul 21 2017 09:50
yup, the concept I mean
yielding some value at some point defers it until execution
Denis Lisov
@tanriol
Jul 21 2017 09:51
@Shaddy Not yet, but something like that is being planned...
Zakarum
@omni-viral
Jul 21 2017 09:51
Generators in python are coroutines. Rust has none
Sherab Giovannini
@Shaddy
Jul 21 2017 09:51
is it executed in an event loop :/?
Zakarum
@omni-viral
Jul 21 2017 09:52
@Shaddy no
Sherab Giovannini
@Shaddy
Jul 21 2017 09:52
I used to think that generators are not coroutines in python
Zakarum
@omni-viral
Jul 21 2017 09:52
They are similar
Denis Lisov
@tanriol
Jul 21 2017 09:53
See the RFC (rust-lang/rfcs#2033)
Zakarum
@omni-viral
Jul 21 2017 09:53
A Python generator is one specific, limited form of coroutines.
Sherab Giovannini
@Shaddy
Jul 21 2017 09:53
thats true
but implementing coroutines in Rust seems more like adding the async paradigmn
RuRu92
@RuRu92
Jul 21 2017 09:54
What are generators or corutiens?
And yeah I did see that rust is getting picked up by the gaming community
Zakarum
@omni-viral
Jul 21 2017 09:54
Cause everthing (async, generators, coroutines) are essentially the same
Sherab Giovannini
@Shaddy
Jul 21 2017 09:55
yep, but usage of generator it's usually less code and simple if you define that limited behaviour apart from asyncs
Zakarum
@omni-viral
Jul 21 2017 09:55
@RuRu92 and try-to-be-game-dev community )
Sherab Giovannini
@Shaddy
Jul 21 2017 09:56
@RuRu92 a generator in python is a function that contains a special keyword "yield" at some point, that acts as a return, but if you execute the function it's not being executed until you start to iterate the result
Zakarum
@omni-viral
Jul 21 2017 09:56
@Shaddy generators and coroutines both requires detaching stack or something like that
Sherab Giovannini
@Shaddy
Jul 21 2017 09:56
dunno if its a good explanation but, it allows you to iterate each step of a loop at the time you're iterating the result of a function
instead of iterating a complete result
I don't know how they work as core design tbh
for me I don't know why but they remind me like chained sprockets of a bike
Zakarum
@omni-viral
Jul 21 2017 09:58

iterate each step of a loop at the time you're iterating the result

is achievable with rust iterators

Sherab Giovannini
@Shaddy
Jul 21 2017 09:58
could be right?
I read at some point that functions doesnt start to "execute" until you iterate them or something like that
so I understood that it could be some kind of "generation"
Zakarum
@omni-viral
Jul 21 2017 10:00
calling the function with yield returns generator object. And calling generator.next() for the first time starts function body execution
generator.next() will return everything you yield from it
one at a time
Sherab Giovannini
@Shaddy
Jul 21 2017 10:01
is it rust or python?
Zakarum
@omni-viral
Jul 21 2017 10:01
python
Sherab Giovannini
@Shaddy
Jul 21 2017 10:01
okok
xD
yes, thats it, better explained :P
so you do one pedaling, and the chained sprockets starts to move :D
Zakarum
@omni-viral
Jul 21 2017 10:03
So. As you see. generator object holds the functions local variables.
It easy for python
Cause it all a simple dict
Sherab Giovannini
@Shaddy
Jul 21 2017 10:03
yes
Zakarum
@omni-viral
Jul 21 2017 10:03
Harder for Rust cause it has to be real stack
Sherab Giovannini
@Shaddy
Jul 21 2017 10:03
agree
Zakarum
@omni-viral
Jul 21 2017 10:04
And you have no idea how big it should be
Sherab Giovannini
@Shaddy
Jul 21 2017 10:04
Python can do whatever they want easily, Rust has to pay a lot for it
but its what makes great Rust
Zakarum
@omni-viral
Jul 21 2017 10:04
python pay at runtime
Sherab Giovannini
@Shaddy
Jul 21 2017 10:04
true
Zakarum
@omni-viral
Jul 21 2017 10:04
But. You can partially emulate generator with iterators and closures in Rust
Sherab Giovannini
@Shaddy
Jul 21 2017 10:05
and also 20 years later it's starting to solve dependencies and versioning problems
which in Rust with crates are perfect
yes
thats true as well
I love the way of "lambda" expressions works in Rust
also love that many or all expressions may return a result
its just awesome
Zakarum
@omni-viral
Jul 21 2017 10:07
All praise the lordRust
Sherab Giovannini
@Shaddy
Jul 21 2017 10:08
hahaha
Sherab Giovannini
@Shaddy
Jul 21 2017 10:16
btw, is there a way to don't specify crate version?
Zakarum
@omni-viral
Jul 21 2017 10:16
cratename = "*"
Sherab Giovannini
@Shaddy
Jul 21 2017 10:16
great! ty
guess that it's appropiate to fit to some fixed version at some point, but for fast coding its great
Andrey Lesnikov
@ozkriff
Jul 21 2017 10:22
crates.io will not allow you to upload a crate with star deps anyway
Ilya Bogdanov
@vitvakatu
Jul 21 2017 10:24

crates.io will not allow you to upload a crate with star deps anyway

and it's good

Sherab Giovannini
@Shaddy
Jul 21 2017 10:25
agree
which is the most efficient u8 container? a vector?
mean, I want to randomly fill 120MB of bytes of memory
RuRu92
@RuRu92
Jul 21 2017 10:30
@omni-viral Where would I want to use generators or corutines? Some usecase? I guess Java doesn't have that, I know C# has a yield keyworld
Zakarum
@omni-viral
Jul 21 2017 10:33
@RuRu92 to write complex iterable objects easier.
For example you can write a function that yields each line of a file instead of reading it from begin to end and splitting in list
In Rust you can do same thing writing special type and implementing Iterator for it
Denis Lisov
@tanriol
Jul 21 2017 10:56
@RuRu92 Coroutines are a nice alternative to callbacks when you need to do some async programming...
Zakarum
@omni-viral
Jul 21 2017 10:59
@tanriol I see coroutines more like an alternatinve to thread-for-each-action approach
Looks similar
RuRu92
@RuRu92
Jul 21 2017 11:10
That makes sense @tanriol
Can someone of you show some example?
Denis Lisov
@tanriol
Jul 21 2017 11:12
@RuRu92 For a nice Python-based example, see Trio tutorial
Michal 'vorner' Vaner
@vorner
Jul 21 2017 11:30
The documentation on docs.rs has that [src] button to the right of the definitions. But when I run cargo doc, I don't get one. Is that normal? Also, I get this error when I run cargo doc:
 Documenting aggregator v0.1.0 (file:///home/vorner/work/pakon/aggregator)
warning: source code was requested to be rendered, but processing `<proc-macro source code>` had an error: No such file or directory (os error 2)
         skipping rendering of source code
Ah, I found an issue for that one. rust-lang/rust#43371
Sherab Giovannini
@Shaddy
Jul 21 2017 11:37
Doesn't std::fs::File have any implentation to easily query for size/len?
Debug/Read/Write/Seek (which can be used to tell size) / etc. but seems that there is no Length trait for IO or something :D
Denis Lisov
@tanriol
Jul 21 2017 11:40
Maybe .metadata()?.len()
Sherab Giovannini
@Shaddy
Jul 21 2017 11:41
but it doesn't work with file object or yes?
oh
ok
xD
nevermind
Sathya Narrayanan
@sourcepirate
Jul 21 2017 13:19
@all how do is send a BigUInt number from num crate over a tcpStream ?
Denis Lisov
@tanriol
Jul 21 2017 13:21
Manually.
Andrey Lesnikov
@ozkriff
Jul 21 2017 13:21
serialize it with serde or something like that
Sathya Narrayanan
@sourcepirate
Jul 21 2017 13:23
the data inside bigint is stored as Vec<BigDigit> . https://docs.rs/num-bigint/0.1.40/src/num_bigint/biguint.rs.html#44-46 . Even if i think to serialize and send the data. How can i deserilize at the other end
What i mean is how can i serialize and deserialize a Vec<T> data. ?
I am using this approch
struct BigInteger(BigInt);

impl Deref for BigInteger {
    type Target = BigInt;
    fn deref(&self) -> &Self::Target {
        &self.0
    }
}

impl ser::Serialize for BigInteger {
    fn serialize<S>(&self, serializer: &mut S) -> Result<()> where S: ser::Serializer
    {
        let (sign, bytes) = self.to_bytes_le();
        let length = bytes.len();
        match length {
            0...255 => self.get_small_big_number(sign, length as u8, bytes),
            _ => self.get_large_big_number(sign, length as u32, bytes),
        };

        Ok(())
    }
}
Is there any better alternative ?
Joonas Koivunen
@koivunej
Jul 21 2017 13:43
@sourcepirate what do get_small_big_number and get_large_big_number do?
Zakarum
@omni-viral
Jul 21 2017 13:44
@sourcepirate couldn't find use of serializer in serialize
Joonas Koivunen
@koivunej
Jul 21 2017 13:48
@sourcepirate i don't really know the num-bigint but there might be some serialization feature already built-in the crate but not enabled by default. if you have to implement this by yourself then length + sign_byte + either byteorder bytes sounds like a good idea
@sourcepirate again, if you must do this by hand, using the sign_byte as a bitmask to specify also the endianess of the following bytes might make it more self-descriptive. you could probably use leb128 for the length field
Sherab Giovannini
@Shaddy
Jul 21 2017 13:55
I did a dumb MB/KB/Bytes converter and went into a question: Why do I need to specify that FileSize is a reference after matching self?
#[derive(Debug)]
pub enum FileSize {
    MB(u32),
    KB(u32),
    Bytes(u32),
}

impl FileSize {
    pub fn in_bytes(&self) -> u32 {
        match self {
            &FileSize::MB(size) => size * ONE_MB,
            &FileSize::KB(size) => size * ONE_KB,
            &FileSize::Bytes(size) => size,
        }
    }

    pub fn in_kb(&self) -> u32 {
        match self {
            &FileSize::MB(size) => size / ONE_KB,
            &FileSize::KB(size) => size,
            &FileSize::Bytes(size) => size * ONE_KB,
        }
    }

    pub fn in_mb(&self) -> u32 {
        match self {
            &FileSize::MB(size) => size,
            &FileSize::KB(size) => size / ONE_KB,
            &FileSize::Bytes(size) => size / ONE_MB,
        }
    }
}
Andrey Lesnikov
@ozkriff
Jul 21 2017 13:57
you can write match *self { FileSize::MB(size) => ...
Sherab Giovannini
@Shaddy
Jul 21 2017 13:58
ok, so I dereference self
ty
Joonas Koivunen
@koivunej
Jul 21 2017 14:03
@Shaddy i did not know about this "trick" (or convention) until clippy showed it to me, recommend checking it out: https://github.com/rust-lang-nursery/rust-clippy
Sathya Narrayanan
@sourcepirate
Jul 21 2017 14:06
@koivunej thanks
Sherab Giovannini
@Shaddy
Jul 21 2017 14:06
oh, thanks @koivunej !
Andrey Lesnikov
@ozkriff
Jul 21 2017 14:07
Sherab Giovannini
@Shaddy
Jul 21 2017 14:07
why is not included directly in core? xD
Andrey Lesnikov
@ozkriff
Jul 21 2017 14:08
Sherab Giovannini
@Shaddy
Jul 21 2017 14:10
the concept of nursery is kinda like "alpha" crates?
or plugins
Sherab Giovannini
@Shaddy
Jul 21 2017 14:14
thanks :)
Andrey Lesnikov
@ozkriff
Jul 21 2017 14:16
TLDR (as I understand it): crates in the nursery are very important for rust's ecosystem and they are maintained by the core team
and someday clippy (together with rustfmt) will be moved into the rustup
Sherab Giovannini
@Shaddy
Jul 21 2017 14:18
I see also that rls, and well, the most relevant packages seems to be under nursery repo
really interesting
lets see if clippy works with nightly
xD
seems that is broken :( :( :(
Andrey Lesnikov
@ozkriff
Jul 21 2017 14:21
rustc -V?
Sherab Giovannini
@Shaddy
Jul 21 2017 14:22
1.19.0
PS J:\dev\rust\testing_bits> rustc -V
rustc 1.19.0 (0ade33941 2017-07-17)

and the errors blame about these features:

box_syntax
alloc
custom_attribute
i128_type
i128
rustc_private
slice_patterns
stmt_expr_attributes
conservative_impl_trait

Andrey Lesnikov
@ozkriff
Jul 21 2017 14:23
you need a fresh nightly to build clippy
$ cargo clippy -V
0.0.144
$ rustc -V
rustc 1.20.0-nightly (582af6e1a 2017-07-19)
Sherab Giovannini
@Shaddy
Jul 21 2017 14:24
ok, let me check
Andrey Lesnikov
@ozkriff
Jul 21 2017 14:25
As a general rule clippy will only work with the latest Rust nightly for now.
Sherab Giovannini
@Shaddy
Jul 21 2017 14:25
yup, just my fault for some reason didn't set default rustup as nigthly
yay! it works!
Sorry for the noisy, but looks promising:
warning: this argument is passed by value, but not consumed in the function body
  --> src\lib.rs:65:37
   |
65 | pub fn create_random_file(filename: String, size: FileSize) -> Result<(), Box<Error>> {
   |                                     ^^^^^^ help: consider changing the type to: `&str`
   |
   = note: #[warn(needless_pass_by_value)] on by default
   = help: for further information visit https://github.com/Manishearth/rust-clippy/wiki#needless_pass_by_value

warning: this argument is passed by value, but not consumed in the function body
  --> src\lib.rs:65:51
   |
65 | pub fn create_random_file(filename: String, size: FileSize) -> Result<(), Box<Error>> {
   |                                                   ^^^^^^^^ help: consider taking a reference instead: `&FileSize`
   |
   = help: for further information visit https://github.com/Manishearth/rust-clippy/wiki#needless_pass_by_value

warning: this argument is passed by value, but not consumed in the function body
  --> src\lib.rs:91:36
   |
91 | pub fn randomly_bit_test(filename: String) -> Result<(), Box<Error>> {
   |                                    ^^^^^^ help: consider changing the type to: `&str`
   |
   = help: for further information visit https://github.com/Manishearth/rust-clippy/wiki#needless_pass_by_value
Andrey Lesnikov
@ozkriff
Jul 21 2017 14:27
sometimes nightly is too fresh (clippy isn't updated yet) so check https://github.com/rust-lang-nursery/rust-clippy/blob/master/CHANGELOG.md before updating clippy
Sherab Giovannini
@Shaddy
Jul 21 2017 14:27
gottcha
Joonas Koivunen
@koivunej
Jul 21 2017 14:37
whoa, haven't used clippy in a while (got stuck on some nightly issue or clippy bug) and i believe that list of checks has grown immensly. can't even remember that "this argument is passed by value, but not consumed in the function body" nag. need to install clippy again to see what it detects nowdays!
Andrey Lesnikov
@ozkriff
Jul 21 2017 14:39
Does anyone know what is @impl in macro? Like in
    (@impl $name:ident) => {
        impl TypeName for $name {
            fn type_name() -> &'static str { stringify!($name) }
        }
    };
Joonas Koivunen
@koivunej
Jul 21 2017 14:43
can't remember seeing @ in a macro_rules! match arm or what ever that is called
@ozkriff do you see any usages of this macro?
looks like this is some kind of meta-macros
Sherab Giovannini
@Shaddy
Jul 21 2017 14:46
wow, this is tier1 rust level :O
Joonas Koivunen
@koivunej
Jul 21 2017 14:47
@ozkriff so it'd seem... i remember looking at error_chain! and quick_error! macro usage but can't remember seeing @ there either. perhaps it's just a part of an identifier, to hide that "private" macro part?
Andrey Lesnikov
@ozkriff
Jul 21 2017 14:50

https://docs.rs/custom_derive/0.1.7/custom_derive/ hmmm

Finally, @impl is merely a trick to pack multiple, different functions into a single macro. The sequence has no special meaning; it is simply distinct from the usual invocation syntax.

Sherab Giovannini
@Shaddy
Jul 21 2017 14:54
is there some kind of function aliasing for impl?
Andrey Lesnikov
@ozkriff
Jul 21 2017 14:59
it's just a special name for one of the rules, as koivunej has suggested
Sherab Giovannini
@Shaddy
Jul 21 2017 14:59
sorry, it's not related with @impl macro xD
meaning if it's there a canonical way to aliasing methods while implementing for structs/enums
Andrey Lesnikov
@ozkriff
Jul 21 2017 15:00
ouch X-|
Sherab Giovannini
@Shaddy
Jul 21 2017 15:02
following the last example, I came into this, but not sure if it's the best way
    pub fn in_mb(&self) -> u32 {
        match *self {
            FileSize::MB(size) | FileSize::MegaBytes(size) => size,
            FileSize::KB(size) | FileSize::KiloBytes(size) => size / KILOBYTE,
            FileSize::BB(size) | FileSize::Bytes(size) => size / MEGABYTE,
        }
    }

    pub fn in_megabytes(&self) -> u32 { self.in_mb() }
Andrey Lesnikov
@ozkriff
Jul 21 2017 15:05
looks fine to me, i don't know any shorter syntax
Sherab Giovannini
@Shaddy
Jul 21 2017 15:06
all right, thanks
David Harvey-Macaulay
@alteous
Jul 21 2017 17:25
@Shaddy FYI the Rust naming convention for FileSize would be Mb, Kb, Bytes, etc.
Sherab Giovannini
@Shaddy
Jul 21 2017 17:42
Ok, thanks for the tip
Anyway it should be renamed to bytesize
Roman Akberov
@RomanAkberov
Jul 21 2017 17:48
@Shaddy You can also put use FileSize::*; at the beginning of the function's body and then just use MB, KB, etc.
Sherab Giovannini
@Shaddy
Jul 21 2017 18:14
Yep I did it in a post refactor, but in a worse way let me check
I had to put it before de impl because didn't allow me, maybe is not allowed without ::*
Sherab Giovannini
@Shaddy
Jul 21 2017 18:21
It works in the beggining of the body, thanks
Sherab Giovannini
@Shaddy
Jul 21 2017 20:35
I'm wondering about how efficient are Rust structs against for example, C++ objects
I know that every benchmark answers Rust, but sometimes is hard to get where the optimizations are
isaacg1
@isaacg1
Jul 21 2017 22:26
I'm confused what's going on with this error message from Cargo:
cargo install cpuprofiler --force
    Updating registry `https://github.com/rust-lang/crates.io-index`
  Installing cpuprofiler v0.0.3
error: specified package has no binaries
Andrey Lesnikov
@ozkriff
Jul 21 2017 22:31
cpuprofiler is a library, not an executable. you can cargo install a package only if it has executables
if you want to use a library in your code - add it to your Cargo.toml: http://doc.crates.io/guide.html#adding-dependencies-from-cratesio
isaacg1
@isaacg1
Jul 21 2017 22:51
@ozkriff Thanks, it looks like I was having an XY problem. I think the actual problem lies with cpuprofiler's non-rust dependencies.
isaacg1
@isaacg1
Jul 21 2017 23:06

The actual error I'm getting is the following, when trying to run the program using cpuprofiler:

target/debug/colors: error while loading shared libraries: libprofiler.so.0: cannot open shared object file: No such file or directory

Andrey Lesnikov
@ozkriff
Jul 21 2017 23:14

have you seen https://github.com/AtheMathmo/cpuprofiler#installation ?

In order to use this library you will need to install gperftools. There are instructions in their repository but it's roughly the following:

  • Download package from releases
  • Run ./configure
  • Run make install