These are chat archives for rust-lang/rust

7th
Feb 2018
Kelly Thomas Kline
@kellytk
Feb 07 2018 05:14
What's the most direct method to use when checking if a String contains only "v"?
Restioson
@Restioson
Feb 07 2018 05:26
@kellytk are you saying the string must be only 'v's or just one character, v
Kelly Thomas Kline
@kellytk
Feb 07 2018 05:27
Any number of "v" characters in the String @restioson
Restioson
@Restioson
Feb 07 2018 05:31
string.chars().take_while(|c| c == 'v').count() == string.len() ?
This message was deleted
Kelly Thomas Kline
@kellytk
Feb 07 2018 05:42
Thanks @Restioson
Dylan DPC
@Dylan-DPC
Feb 07 2018 06:52
@kellytk I think there is a contains function in String
Kelly Thomas Kline
@kellytk
Feb 07 2018 06:53
@Dylan-DPC Wouldn't it, for "v", find a false positive with "vr" because it contains "v", but "r" as well?
Dylan DPC
@Dylan-DPC
Feb 07 2018 06:56
Yes it would. Not sure what you exactly want here but you can skim through all the string functions you might find something that suits your purpose
Kelly Thomas Kline
@kellytk
Feb 07 2018 06:58
Exclusively "v"; @Restioson pointed me in the direction of a solution, thanks
Dylan DPC
@Dylan-DPC
Feb 07 2018 07:01
Cool
Antonin Carette
@k0pernicus
Feb 07 2018 07:58
Hello there!
I tried two code yesterday, using C and Rust. This code makes just stupid think: it updates a variable, and return this variable as soon as the towers of Hanoi have been resolved.
Using Rust and -O2/-O3, it works very well and it seems that the run time is constant (even for big numbers)
Using C and -O2/-O3, it works well for towers < 15, and it seems that it work a loooooong time for towers > 5
Do you know the difference between Rust compiler optimization for recursion and C one (clang) please ?
Jente Van Heuverswyn
@JenteVH
Feb 07 2018 08:12

Hi all.

Coming from web development I got spoiled with tools such as hot module replacement etc.
What does a developer workflow generally look like for a language like rust?
Are you supposed to turn off your program -> build -> run program -> see if your changes are correct?
This seems like a lot of overhead to me. Am I missing something here?

Joonas Koivunen
@koivunej
Feb 07 2018 08:22
@k0pernicus I haven't seen an overview of the optimizations in rust. perhaps you could profile your two versions and find out why the C-version is so much slower, then look at the disassembly to see the differences. also, it might be a good idea to post your sources in a gist when more knowledgeable people come around, they might be able to point out the source of the difference simply looking at the implementation..
Antonin Carette
@k0pernicus
Feb 07 2018 08:23
Hello @koivunej
Yes, sorry
This is the pastebin for C: https://pastebin.com/dXAwj8ZT
Joonas Koivunen
@koivunej
Feb 07 2018 08:25
@JenteVH I haven't done any rust web development (yet) but in general there have been attempts at hot code swap by using dynamic libraries in the context of game development (possibly at users.rust-lang.org); nothing "development process" level ready as far as i know. the programs I've written I've tried to design so that you'll get the most benefit from the compiler and try to fill in the gaps with test cases, so you need less manual testing.
Antonin Carette
@k0pernicus
Feb 07 2018 08:26
And the one for Rust: https://pastebin.com/T8KdKw3M
Jente Van Heuverswyn
@JenteVH
Feb 07 2018 08:27
@koivunej Thank you for the insight. I meant general development though, not really specific to web (but could be). So the bottom line is that it's being worked on but for now you kind of have to 'deal with it'?
Joonas Koivunen
@koivunej
Feb 07 2018 10:04
@JenteVH I'm not so sure if hot swapping is actively being worked on. I'd say my solution is to rely on automated tests instead of manually running the end result for feedback.
Jente Van Heuverswyn
@JenteVH
Feb 07 2018 10:05
@koivunej Alright. Better dive into testing in rust then
Adel Prokurov
@aprokurov
Feb 07 2018 15:39
How i can made this work?
let _text = |s| s.call_on_id("text", |view1: &mut TextArea|{
                            view1.get_content().to_string()
                        }).unwrap();
error[E0619]: the type of this value must be known in this context
   --> src/main.rs:156:33
    |
156 |                   let _text = |s| s.call_on_id("text", |view1: &mut TextArea|{
    |  _________________________________^
157 | |                             view1.get_content().to_string()
158 | |                         }).unwrap();
    | |__________________________^
Dylan DPC
@Dylan-DPC
Feb 07 2018 15:58
simple
just do | s: WhateverType |
should do the trick
@aprokurov
Daniel Bischof
@dbischof90
Feb 07 2018 19:10
I have a little problem that 'm trying to figure out. I'm getting the following error:
no method named `map` found for type `&std::vec::Vec<another_lib::MyStruct>` in the current scope
Which I find a little strange - after all I'm just trying to use map on a Vector
Nate Mara
@natemara
Feb 07 2018 19:10
I believe you need to call .iter() on the vec before you can map
Steve Klabnik
@steveklabnik
Feb 07 2018 19:10
yup
map is on iterators, vectors are not iterators
Daniel Bischof
@dbischof90
Feb 07 2018 19:11
Ah, taking a second look at the error message, they do actually hint iterators. Thanks guys.
Antonin Carette
@k0pernicus
Feb 07 2018 20:07
@steveklabnik Do you know if the rust compiler performs some optimizations for recursive functions?
Steve Klabnik
@steveklabnik
Feb 07 2018 20:07
llvm will do TCO if it thinks it can
there's no way to guarnatee it
Antonin Carette
@k0pernicus
Feb 07 2018 20:08
Ok, like gcc -O2 I think...
Steve Klabnik
@steveklabnik
Feb 07 2018 20:08
there's an RFC to do so, but it's been moved to "postponed"
Antonin Carette
@k0pernicus
Feb 07 2018 20:08
Oh ok
Steve Klabnik
@steveklabnik
Feb 07 2018 20:08
you'd use a new keyword to say "hey this is a tail call and fail compilation if not so"
it just doesn't fit into this year's roadmap, hence the postponement
it's still something we'd eventually like
Antonin Carette
@k0pernicus
Feb 07 2018 20:09
Actually, I try to understand why my rust code, which contains a recursive function which returns an integer value, performs at constant time even for big recursive calls - the C code is so much slower than the rust code...
Steve Klabnik
@steveklabnik
Feb 07 2018 20:09
only way to be sure is to look at the asm
but that might be what's going on, yeah
Antonin Carette
@k0pernicus
Feb 07 2018 20:10
Yep, I tried - the C code is clear but the asm from the Rust is… huge
So I tried to explore the LLVM IR but… huge also (and a few knowledge)
Steve Klabnik
@steveklabnik
Feb 07 2018 20:12
how many dependencies does the code have
you might be able to toss them in godbolt
Antonin Carette
@k0pernicus
Feb 07 2018 20:12
No one
Steve Klabnik
@steveklabnik
Feb 07 2018 20:12
ah! yeah, use godbolt
itll help a lot
Antonin Carette
@k0pernicus
Feb 07 2018 20:13
Ok, I will check that :-)
Steve Klabnik
@steveklabnik
Feb 07 2018 20:13
Antonin Carette
@k0pernicus
Feb 07 2018 20:13
Thanks a lot
Steve Klabnik
@steveklabnik
Feb 07 2018 20:13
basically, it's like the playpen, except that
  1. it tries to remove irrelevant assembly
  1. it tries to link your code to the assemly, so you can just click the function and it'll go to it in the output
Antonin Carette
@k0pernicus
Feb 07 2018 20:14
Oh wow!
It’s awesome
Steve Klabnik
@steveklabnik
Feb 07 2018 20:14
:metal:
Denis Lisov
@tanriol
Feb 07 2018 20:17
@k0pernicus The same Hanoi code you've linked to above?
Antonin Carette
@k0pernicus
Feb 07 2018 20:17
@tanriol Yes
There is the C and the Rust version
All in Pastbin
Steve Klabnik
@steveklabnik
Feb 07 2018 20:17
you can even diff them
from what i hear
Antonin Carette
@k0pernicus
Feb 07 2018 20:18
like get out the LLVM IR from the C and Rust code and get the diff ?
Steve Klabnik
@steveklabnik
Feb 07 2018 20:18
the asm
Denis Lisov
@tanriol
Feb 07 2018 20:18
IIUC, for the Rust version on release LLVM optimizes out the second recursive call, making it linear, not exponential.
Antonin Carette
@k0pernicus
Feb 07 2018 20:19
@steveklabnik I will try, thanks :-)
@tanriol That was my first hypothesis
Sergey Noskov
@Albibek
Feb 07 2018 20:20
Hi, guys. Is this possible to have a feature in dependent crate enabled only if some feature in my crate is enabled too? For example, I want uuid's serde to be turned on only if my crate enables serde as well.
Steve Klabnik
@steveklabnik
Feb 07 2018 20:21
@k0pernicus https://godbolt.org/g/ywthHt <- note i changed the code so there is a diff :)
Denis Lisov
@tanriol
Feb 07 2018 20:22
@Albibek Yes, you can have
[features]
serde = ["uuid/serde"]
Not "only if", actually, but "if any crate that depends on it requires that feature".
Sergey Noskov
@Albibek
Feb 07 2018 20:24
@tanriol thanks! It worked!
Steve Klabnik
@steveklabnik
Feb 07 2018 20:25
https://godbolt.org/g/2QZ4gS <- a better one, with optimizations and without debug info so rust doesn't generate frame pointers
Antonin Carette
@k0pernicus
Feb 07 2018 20:28
Thanks @steveklabnik - I am studying the diff :-)
Steve Klabnik
@steveklabnik
Feb 07 2018 20:29
:+1:
Antonin Carette
@k0pernicus
Feb 07 2018 20:37
So, the C code asm performs two calls to the function move; the Rust code asm performs only one call to the function move, and some jmp after this call
It tries to inline the code in order to compute the final value - smarter than gcc...
dunnousername
@dunnousername
Feb 07 2018 23:09
I'm trying to make a pseudocode to XYZ compiler, so I'm trying to define that an expression can either be empty, a constant, a variable, or an operation of two expressions. I get an 'infinite in size' error for Expression, so I'm not sure how else to define it. https://pastebin.com/WiNYS8fq
My head really hurts now, with types and instances of types and types that when instantiated would not have a value...
Alexandre Bury
@gyscos
Feb 07 2018 23:11
It's a common problem with recursive enums
Denis Lisov
@tanriol
Feb 07 2018 23:11
The expressions should probably contain Box<Expression>, not just Expression
dunnousername
@dunnousername
Feb 07 2018 23:11
I'm trying to make an AST
Alexandre Bury
@gyscos
Feb 07 2018 23:11
You need some indirection. Box is the simplest way to get there.
dunnousername
@dunnousername
Feb 07 2018 23:11
I'll try that
This reminds me of my old malloc function. Never again will I touch that code
dunnousername
@dunnousername
Feb 07 2018 23:15
So, does An<T> correctly describe a value that is known at compile time or runtime? I'm not sure what I wrote just now haha
Alexandre Bury
@gyscos
Feb 07 2018 23:15
At compile time
dunnousername
@dunnousername
Feb 07 2018 23:15
In An<T>, T is a type, but in Const(T), T is a value?
Say An<u32>
Const(42)
Alexandre Bury
@gyscos
Feb 07 2018 23:16
Yes
An<T> means the type depends on a type T (here u32)
Const(T) means Const is a tuple of type (T), which here is (u32)
Such a tuple is initialized by a value of type T, like 42
dunnousername
@dunnousername
Feb 07 2018 23:17
Does my Var carry the type information of the enum?
So a Var of An<u32> is different from a Var of An<i32>
Alexandre Bury
@gyscos
Feb 07 2018 23:17
yes
It's entirely different types
Like in C++
dunnousername
@dunnousername
Feb 07 2018 23:17
Ok cool
Can I assert that I can, for example, only use the = operator on Vars?
dunnousername
@dunnousername
Feb 07 2018 23:22
In the enum
Alexandre Bury
@gyscos
Feb 07 2018 23:45
I'm not sure what you mean
dunnousername
@dunnousername
Feb 07 2018 23:46
Nevermind