These are chat archives for rust-lang/rust

19th
Jun 2018
Paul Masurel
@fulmicoton
Jun 19 2018 00:05
How do I get the failed assert log in a doctest?
right now my doctest fail silently
Devashish Dixit
@devashishdxt
Jun 19 2018 07:36

How to do this in Rust

match x {
    1 => {
        // do foo stuff
    },
    2 => {
        // do bar stuff
    },
    _ => {
        // do baz stuff
        // do bar stuff
    }
}

Here in cases 2 and _ I have to do some stuff. But, in case of _ I also have to do some more stuff. How can I do this without duplicating code?

In C or C++ it is possible by not using break;
Andrey Lesnikov
@ozkriff
Jun 19 2018 07:48
rust's match doesn't support fallthrough. depending on details, either use a sequence of if statements, or extract a common code into a separate function and just call it multiple times
Alexander Larin
@scalar438
Jun 19 2018 08:37
How to do this in C++? It is obvious only if you swap "baz" and "bar" in last case.
Fredrik Portström
@portstrom
Jun 19 2018 08:42
if x == 1 {
    unimplemented!("foo");
} else {
    unimplemented!("baz");
    if x == 2 {
        unimplemented!("bar");
    }
}
Same goes in any language.
trsh
@trsh
Jun 19 2018 09:47
@portstrom I think they know that, and are just looking for something similar to Switch
Fredrik Portström
@portstrom
Jun 19 2018 09:48
@trsh Often people don't realize they don't need what they're looking for, because there's a simpler solution.
Especially the switch statement I think people use just because it is there, not because they actually need it. Because it's become such a strong habit after many years of use, they don't realize they don't need it.
When I was programming in JS, I had a linter rule forbidding switch statements, because they are rarely if ever warranted.
Björn Lindström
@bkhl
Jun 19 2018 10:04
Agree. If you're going to have anything like a switch statement, it should look like match in Rust. :-)
trsh
@trsh
Jun 19 2018 10:10
Agree
trsh
@trsh
Jun 19 2018 11:29
How do I turn a slice from array into owned cloned vector?
trsh
@trsh
Jun 19 2018 11:31
@portstrom yeah, tnx
Krys
@Krysme
Jun 19 2018 12:34
Hey, a quick question here, I know that rust uses null reference optimization to make an Option type the same size as the original one, so can I make an invalid value of my own type and influence the optimization process to treat my invalid value as None ?
Fredrik Portström
@portstrom
Jun 19 2018 12:39
@Krysme Rust applies the same optimization to your own types. Try it with std::mem::size_of if unsure. http://play.rust-lang.org/?gist=be4854af6eaa96380ee73c35724a6a05
Sylwester Rąpała
@xoac
Jun 19 2018 12:41
awesome <3
Sylwester Rąpała
@xoac
Jun 19 2018 13:03
I do a simple program that works with IO (serial). It works nice (for now). I would like to add REST API to that application to write microservices. Is any crate that is recommend to be used with tokio?
I checked(just reading about, not tested): hyper (to low level), actix and rocket?
Fredrik Portström
@portstrom
Jun 19 2018 13:06
Hyper is the one crate for HTTP to use with Tokio. It would be hard to find a reason to say it's too low level. It's basically the same as the new high level fetch API used in browsers.
Denis Lisov
@tanriol
Jun 19 2018 13:08
Do you need an HTTP client or server?
Sylwester Rąpała
@xoac
Jun 19 2018 13:08
On the start just server (but I know I would need also client in the future)
@portstrom I would like to write this API until end of day. So hyper probably need more time to understand I guess.
Denis Lisov
@tanriol
Jun 19 2018 13:11
I'd look at either actix-web or gotham for server.
Krys
@Krysme
Jun 19 2018 13:11
@portstrom Well, the invalid type might not be zero though. For instance, I might have a type like Age with the invalid value 255, and a RawOsSocket that never equals to -1 when valid. So, how do I tell the compiler so it knows to treat -1 or 255 as None ?
Fredrik Portström
@portstrom
Jun 19 2018 13:15

@Krysme You don't. But do you ever need in real programs? The age example* seems artificial. The raw socket is probably going to have something more in the same structure to make it less raw, for a example a non-null pointer to a buffer.

*https://stackoverflow.com/questions/30414068/can-i-use-the-null-pointer-optimization-for-my-own-non-pointer-types

Fredrik Portström
@portstrom
Jun 19 2018 13:21
For server it may arguably make sense to have something extra to handle routing and such. But do you really need it? Since you're just writing a microservice, having Rocket with fancy macros to declare your routes probably won't be any better than just writing a match expression directly on the request you get from Hyper. Also keep in mind that these frameworks are experimental and come and go just like fashion. Hyper itself however is timeless and will stay.
Fredrik Portström
@portstrom
Jun 19 2018 13:27
Forget about Rest. Scattering request data throughout the HTTP method, path, query string and request body and then spreading your response data between the HTTP status code and the response body is just a crap idea. Simply put everything in the request body and response body and declare these as structs and add some Serde JSON and #derive and you're done. That way you have strongly typed requests and responses and it's hard to get anything wrong, and you don't have to write tons of validation code.
Sylwester Rąpała
@xoac
Jun 19 2018 13:38
@portstrom I don't decide to use REST or not. If my app will take a few request per second (max) I think it's fine to match against path and what is more this can be easy used with browser. I would also have a nice way to changing response using "json" and raw if necessary. If u know better option than REST u can link me I am happy to learn. Requirement is can be easy handled by human.
Fredrik Portström
@portstrom
Jun 19 2018 13:43
I think I'll have to make a tutorial sometime how to use the cool features of Rust to make JSON requests safe and easy.
Sylwester Rąpała
@xoac
Jun 19 2018 13:43
And I agree with you that Rest isn't prefect solution but I would go further and say that http could be also replaced with protocol that is simpler and quicker
@portstrom Happy to read that
and I would say msgpack is also better than json if human is not involved
Fredrik Portström
@portstrom
Jun 19 2018 13:46
Msgpack is still too human, carrying plain text keys around. Protocol Buffers is cooler.
Sylwester Rąpała
@xoac
Jun 19 2018 13:48
msgpack allow you to easy migrate from json and if that necessary switch back to json or handle both json and msgpack. json for human and msgpack for other apps
I would need to try protocol buffers
Fredrik Portström
@portstrom
Jun 19 2018 13:49
Yes, but then what's the benefit if msgpack can't do anything that JSON can't? If you use any of the binary types, it's no longer interchangeable with JSON.
If you're looking to make things smaller, there's Zstandard with preshared dictionaries.
Fredrik Portström
@portstrom
Jun 19 2018 13:55

Protocol Buffers has more recently got competition from other formats that have some of the benefits of JSON, not needing preshared schemas: https://avro.apache.org/docs/current/

I would encourage looking through all formats supported by Serde: https://serde.rs

Dmitriy
@dpogretskiy
Jun 19 2018 14:12
fiy impl Trait can be used for cross-platform stuff https://gist.github.com/rust-play/88c2d7892202b1c816683dc53c582c8d
Sylwester Rąpała
@xoac
Jun 19 2018 14:36
@portstrom If u write your tutorial about "use the cool features of Rust to make JSON requests safe and easy." And remember about me please notify me :P
Fredrik Portström
@portstrom
Jun 19 2018 14:37
@xoac Will notify you if I ever write it :)
trsh
@trsh
Jun 19 2018 15:05
@xoac I use actix-web with Juniper back-end. Front-end Angular + Apollo.
It's like dreams come true
Whenever I change backend, I run special sync script front-end that generates type-script objects for GraphQL. If soemthing doesn't fit, I got an error in lint.
With Rest you have to relay on strong memory or docs.. And then you really can't test back-front integrity.
Fredrik Portström
@portstrom
Jun 19 2018 15:09
That's unrelated to Rest. It's just the tools you use for it.
trsh
@trsh
Jun 19 2018 15:10
Tools Shmools, in GQL it's all in the box
Fredrik Portström
@portstrom
Jun 19 2018 15:11
It wasn't in the box when I used it.
Someone changed the server and then the client broke because GQL rejected the request, even in cases Rest wouldn't have broken.
trsh
@trsh
Jun 19 2018 15:12
Im not aware of advanced REST setup, where you change one letter of return object and you can sync that to front-end, and get and error.
In GQL its the Base Idea
In old times we had WSDL
Same idea but with scary XML's
Fredrik Portström
@portstrom
Jun 19 2018 15:15
In present times there is JSON schema.
trsh
@trsh
Jun 19 2018 15:15
Someone changed the server and then the client broke because GQL rejected the request, even in cases Rest wouldn't have broken. > I woud say this is unrelated to GQL :D
Maybe saying 'all in box' is to loud, but the setup was really straight forward
Fredrik Portström
@portstrom
Jun 19 2018 15:17
The client has to tell the server in the request what fields it expects in the response. :worried: And then the server rejects the request. :facepunch: In Rest that just doesn't happen.
trsh
@trsh
Jun 19 2018 15:19
Anyway GQL opens big opportunities for back-end - front-end integrity. Because u can validate against schema. For simple project, where want to just make some call and get some data, I would not recommend it, because it's a little bit complicated.
@portstrom the scenario you described, is one I want. If client doens't know what it asks, or asks it wrong, can go to hell :D
Fredrik Portström
@portstrom
Jun 19 2018 15:20
You can validate without a schema also without GQL, so it makes no difference, but it has all the hype, because people didn't know before GQL got famous that they can validate against a schema.
trsh
@trsh
Jun 19 2018 15:21
If I ask for bananas, and get oranges it will go wrong somwhere in the pipe anyway
Fredrik Portström
@portstrom
Jun 19 2018 15:21
The client knows what it wants. That's not the issue.
trsh
@trsh
Jun 19 2018 15:22
Then I see no problem, besides some server bugs, maybe
I will never look back to rest, because especially when you work in a team. Some dude changes the reponse object name, forgets to notify anybody, and page goes to hell. Then you have to spend much of the hours to Write objects, that will represent the response, and then edit them.
Fredrik Portström
@portstrom
Jun 19 2018 15:26
On the project I unfortunately had GQL, we didn't just have server bugs, but coworkers producing server bugs and debates about how to deploy different versions and refactor the schema. The client supported some optional fields that had not yet been deployed to the server, but GQL doesn't allow retrieving them without explicitly asking for them, and doesn't allow asking for them unless supported by the server.
GQL in a team was a nightmare. After tolerating it for too long I quit.
trsh
@trsh
Jun 19 2018 15:27
Sound more like a team issue, not technology
And yes GQL is strict, and I like it
Fredrik Portström
@portstrom
Jun 19 2018 15:29
Meanwhile the non-GQL routes always worked without breaking. :sweat_smile:
trsh
@trsh
Jun 19 2018 15:29
No optionalities
Its up to project what to use
For my new project its perfect
Million problems gone :D
trsh
@trsh
Jun 19 2018 15:44
@portstrom with schemas it goes like this. 1) There is no point of asking for fields that doesn't exist on server side. You have to align server with client. Rest doesn't care, but i can go wrong deeper in the pipe. 2) When planing to remove or change some fields, a) if its big change, run old (mark depr) and new schema parallel, b) if it small but breaking, first change fields to Deprected, and as time goes by, change..
If the team can align server with client versions, it will go wrong anyway at some sunny day
Fredrik Portström
@portstrom
Jun 19 2018 15:47
The motivation to using GQL was the belief it frees you from having to align versions.
trsh
@trsh
Jun 19 2018 15:48
but nothing does
rally
Fredrik Portström
@portstrom
Jun 19 2018 15:49
It did however introduce versioning problems that we didn't have before.
trsh
@trsh
Jun 19 2018 15:49
one day its optional field (don't care), another it wil be something that will blow up the logic
So better have a blocker, that motivates team to align versions
gql it like rust, and rest like javascript :D
Fredrik Portström
@portstrom
Jun 19 2018 15:51
If Rust was anything like GQL I wouldn't touch it. :poop:
trsh
@trsh
Jun 19 2018 15:51
:D
But javascript allows you todo shit, just like rest. gql doesn't. So that was the comparison, nothing else.
Me gone
Denis Lisov
@tanriol
Jun 19 2018 15:54
You can have a schema with REST too :-)
trsh
@trsh
Jun 19 2018 16:05
@tanriol then you want get rid of 100 enpoint links /getthis /getthat and sync calls too. Then you want paging for nested objects, and /or not writing a big dump querys, that get unreadable . Then you don't want to use 100 tools, to make schema happen and not host seperate page for REST docs. Then you just use gql. :D
Fredrik Portström
@portstrom
Jun 19 2018 16:14
It's all very hypothetical and taken out of thin air.
– You get rid of 100 paths, but what do you get instead? 100, 200 or 700 fields? Is that better or worse?
– You want paging, and then what? Paging is trivial to implement without GQL. With GQL you may not be able to do it without breaking the schema.
– 100 tools. Really? Whether you're using a GQL documentation tool or another documentation tool it's just one tool.
http://dilbert.com/strip/2008-05-08
Fredrik Portström
@portstrom
Jun 19 2018 16:55
If you really want to impress me you should invent WAQL. Web Assembly Query Language :smile:
Instead of having a limiting query language like SQL, it allows you to write your queries in any language and compile them to Web Assembly. The server can safely execute Web Assembly code from untrusted clients enforcing resource limits.
Maik Klein
@MaikKlein
Jun 19 2018 16:56
Does anyone know the name of the font of the logo? https://www.rust-lang.org/logos/rust-logo-blk.svg
Zack Pierce
@ZackPierce
Jun 19 2018 19:14
"WhatTheFont!" suggests it looks somewhat like Antique Extended
But if I had to guess, it's something custom and tweaked. If you really want to know, perhaps the Mozilla trademark people could help: https://www.rust-lang.org/en-US/legal.html , since they claim stewardship over the logo
Sylwester Rąpała
@xoac
Jun 19 2018 20:57
I checked actix-web tutorial And find only example of using shared state by global variable. is is possible to create sth like structure that handle my staff and do know how to send request to underling IO. Btw actix-web can be changed
but quick look at rocket there is also no such example
Sylwester Rąpała
@xoac
Jun 19 2018 21:07
so only a gloabal variable can be used to share states?
Denis Lisov
@tanriol
Jun 19 2018 21:08
Surely not. Are you looking at using actix-web?
Sylwester Rąpała
@xoac
Jun 19 2018 21:08
yes
Denis Lisov
@tanriol
Jun 19 2018 21:09
Have you seen the state example?
Sylwester Rąpała
@xoac
Jun 19 2018 21:13
No, thank you, I just look here for examples folder I see there are more interesting examples :)
Sylwester Rąpała
@xoac
Jun 19 2018 21:18
Sylwester Rąpała
@xoac
Jun 19 2018 22:19

I don't get it:

#[macro_use] extern crate serde_derive;
use actix_web::{http, App, Json, Path, Query, Result};

#[derive(Deserialize)]
struct Info {
    username: String,
}

/// extract path info using serde
fn index(
    info: (Path<Info>, Query<HashMap<String, String>>, Json<Info>),
) -> Result<String> {
    Ok(format!("Welcome {}!", info.0.username))
}

fn main() {
    let app = App::new().resource(
        "/{username}/index.html", // <- define path parameters
        |r| r.method(http::Method::GET).with(index),
    ); // <- use `with` extractor
}

how the with method know how to pares it? what if I change order in index tuple?

Denis Lisov
@tanriol
Jun 19 2018 22:23
The with method "knows" the type of the argument it needs to produce, and extracts it using the FromRequest implementation. There's a set of implementations for tuples where every element implements FromRequest that works like "extract every element and build a tuple of them".
If you change the order of the types, the order of values will change to match.
Kelly Thomas Kline
@kellytk
Jun 19 2018 22:27
Does Clippy run on crates used by a project?
Sylwester Rąpała
@xoac
Jun 19 2018 22:29
I think yes. I had git submodule added by path in Cargo.toml and clippy show me warnings
Kelly Thomas Kline
@kellytk
Jun 19 2018 22:30
Thanks
Sylwester Rąpała
@xoac
Jun 19 2018 22:31
don't treat is a 100% sure answer.
Kelly Thomas Kline
@kellytk
Jun 19 2018 22:31
Noted
Sylwester Rąpała
@xoac
Jun 19 2018 22:31
I used clippy today first time :)