These are chat archives for rust-lang/rust

28th
Sep 2018
trsh
@trsh
Sep 28 2018 08:45
Is there such thing as string enum in RUST
Something.XXX (get String)
?
Thiez
@Thiez
Sep 28 2018 08:50
I'm not sure what you mean, could you give an example of a string enum in another language?
trsh
@trsh
Sep 28 2018 08:54
Or number
Typescript example
enum Response {
No = 0,
Yes = 1,
}
Then I can do Response.No
Use Response as type, has always to be 0 or 1
As far as I am aware there is no such thing for strings, but you could of course implement a method on your enum that returns the correct string for each possibility.
[derive(Copy, Clone, Eq, PartialEq)]
enum Response {
  No,
  Yes,
}

impl Response {
  fn get_string(self) -> &'static str {
    match self {
      Response::No => "No",
      Response::Yes => "Yes",
    }
  }
}
Aem, that should be #[derive( but you get the gist
trsh
@trsh
Sep 28 2018 09:05
@Thiez
trsh
@trsh
Sep 28 2018 10:23
How can I cast i64 to i32, when I know for sure it will not overflow
?
red75prime
@red75prime
Sep 28 2018 10:24
variable as i32
trsh
@trsh
Sep 28 2018 10:26
Doesn't work
Or...
Ups
Sylwester Rąpała
@xoac
Sep 28 2018 11:50
Was there idea of trait As in Rust similar to Into? but taking &self instead of self?
Thiez
@Thiez
Sep 28 2018 11:51
You don't mean AsRef?
Sylwester Rąpała
@xoac
Sep 28 2018 11:53
yep. You are right
Ash
@ashthespy
Sep 28 2018 12:00
Is there a simper way to do this?
    let protofiles = fs::read_dir("proto").unwrap()
            .map(|entry| entry.unwrap().path())
            .collect::<Vec<_>>();

    let files_names = protofiles.iter()
                      .map(|file| file.to_str())
                      .map(|f| f.unwrap())
                      .collect::<Vec<_>>();
Thiez
@Thiez
Sep 28 2018 12:05
.map(|file| file.to_str()).map(|f| f.unwrap()) == .map(|file| file.to_str().unwrap() ) so that's one less line
apart from that I don't think so, but you could of course put that code into a function and then you never have to type it again :p
Ash
@ashthespy
Sep 28 2018 12:06
I could also do it in a for loop?
Thiez
@Thiez
Sep 28 2018 12:08
I guess?
let (mut protofiles, mut files_names) = (vec![], vec![]);
for entry in fs::read_dir("proto").unwrap() {
    let path = entry.unwrap().path();
}
oops
aehm, well then you just do protofiles.push(path); files_names.push(path.to_str().unwrap()) or something along those lines
Ash
@ashthespy
Sep 28 2018 12:11
Yep - but was wondering what is the more "Rustish" way to do this :-)
Thiez
@Thiez
Sep 28 2018 12:14
I prefer your first version but I think it's mostly a matter of personal preference. Either version is fine.
Ash
@ashthespy
Sep 28 2018 12:16
Alright!
Ash
@ashthespy
Sep 28 2018 13:15

I am really struggling today:

const SRC: &str = "File1 File2 FileA FileX FileZ";
let input_files = SRC.split(" ")
                .map(|f| format!("proto/{}.proto",f).as_str())
                .collect::<Vec<&str>>();
/build.rs:20:26
   |
20 |                 .map(|f| format!("proto/{}.proto",f).as_str())
   |                          ^^^^^^^^^^^^^^^^^^^^^^^^^^^        - temporary value dropped here while still borrowed
   |                          |
   |                          temporary value does not live long enough
...
31 | }
   | - temporary value needs to live until here

There has to be a more straight forward way of doing this?

Lyle Mantooth
@IslandUsurper
Sep 28 2018 13:18
Make it a Vec<String>, and then map that to String::as_str() whenever you get values out of it.
But input_files has to live for as long as you're going to use those values.
Ash
@ashthespy
Sep 28 2018 13:20
it feels a bit weird to go from &str to String and then back to &str
or is that normal?
Zakarum
@omni-viral
Sep 28 2018 13:21
You already trying to do. Cause format!() returns String
When you need to create new string you can't make it without some kind of owned string type.
Which is String for instance
Ash
@ashthespy
Sep 28 2018 13:23
I tried first with concat! but that works only with literals.
Zakarum
@omni-viral
Sep 28 2018 13:24
Yes. Cause concat! works at compile time
Ash
@ashthespy
Sep 28 2018 13:24
It seems rather convoluted:
let input_files = SRC.split(" ")
                .map(|f| format!("proto/{}.proto",f))
                .collect::<Vec<String>>();
let files = input_files
                    .iter()
                    .map(|s| s.as_str())
                    .collect::<Vec<&str>>();
or am I not seeing and easier way?
Lyle Mantooth
@IslandUsurper
Sep 28 2018 13:24
Why do you need a Vec<&str> specifically?
Zakarum
@omni-viral
Sep 28 2018 13:25
Yeah. Maybe you can go with Vec<String>?
Lyle Mantooth
@IslandUsurper
Sep 28 2018 13:26
(I like saying .map(String::as_str) when I do this, but it's usually on an Option or Result for a comparison and I use the actual String later.)
Zakarum
@omni-viral
Sep 28 2018 13:27
If API requires slice of slices then you have no other choice
Ash
@ashthespy
Sep 28 2018 13:28
Would there then be a more succulent version of my last snippet?
Lyle Mantooth
@IslandUsurper
Sep 28 2018 13:29
Possibly having SRC contain the path and extension in the first place.
Zakarum
@omni-viral
Sep 28 2018 13:29
Funny how later protoc converts your strs back to String
Ash
@ashthespy
Sep 28 2018 13:29
some compile time voodoo?
Lyle Mantooth
@IslandUsurper
Sep 28 2018 13:30
No, just literally having it all there.
Ash
@ashthespy
Sep 28 2018 13:30
yeah - but it feels like cheating :P
Lyle Mantooth
@IslandUsurper
Sep 28 2018 13:30
We're programmers. That's what we do.
Ash
@ashthespy
Sep 28 2018 13:31
hahah!
Zakarum
@omni-viral
Sep 28 2018 13:32
I'd guess that this is not a bottleneck and you can just allocate vector of strings and continue implementing whatever you are implementing
Ash
@ashthespy
Sep 28 2018 13:34
no it isn't really - I was just wondering if I was doing it in the "right" way.
but whatever works!
Ogulcan Erduran
@erduranogulcan
Sep 28 2018 18:36
Is there any front end language good with rust like elm
Ichoran
@Ichoran
Sep 28 2018 18:45
I'm not quite sure what the question is. You can use Rust with Elm if you want.
If you want to share complex data structures between the frontend and backend, the best "front end language" is Rust itself compiled to wasm and/or js.
Paul Reesman
@reesmanp
Sep 28 2018 20:06
I am trying to impl a trait for FnOnce but need the trait to be available for both FnOnce() and FnOnce(u32, u32) -> String. If I just implement it for FnOnce then (*self)(arg_one, arg_two) fails with expected 0 paramters and when I try to implement it for FnOnce(u32, u32) -> String the compiler tells me that there is a conflicting implementation between it and FnOnce().
Am I over-engineering this or is there a good way to handle this?
Kelly Thomas Kline
@kellytk
Sep 28 2018 20:14
How can NamedFile be made an appropriate return value in https://ghostbin.com/paste/9awcj#L15 ? It's a question related to actix web but it's ultimately a type system question