These are chat archives for rust-lang/rust

10th
Jul 2017
Daan Rijks
@ijks
Jul 10 2017 00:03

@stevensonmt Yes, that's correct. But it's not restricted to just methods, as in, functions in a trait don't have to take Self. A (contrived) example:

trait Gimme<T> {
    fn gimme() -> T;
}

struct GimmeInt;

impl Gimme<i32> for GimmeInt {
    fn gimme() -> i32 {
        42
    }
}

You can then call it as:

println!("{}", GimmeInt::gimme());

Or, if GimmeInt for some reason already has gimme() as a function, e.g. in a normal impl block or from another trait, you can do:

println!("{}", <GimmeInt as Gimme<i32>>::gimme());

Longer example: https://is.gd/Lkgd7h

stevensonmt
@stevensonmt
Jul 10 2017 00:13
@ijks Thanks so much for the clarification and example.
Jente Van Heuverswyn
@JenteVH
Jul 10 2017 09:03

I'm reading through the rust documentation here and got to ownership.

let s1 = String::from("hello");
let s2 = s1;

As explained by the docs, s1 gets moved to s2. This makes it so that you cannot use s1 anymore.
Could anyone give me a practical use for this? Why don't you just keep using s1?

Jonas Platte
@jplatte
Jul 10 2017 09:05
@JenteVH That's not really something you do, it's just an example to illustrate the mechanics. Usually a move occurs because you pattern match on an owned value, or pass it into a function.
David Harvey-Macaulay
@alteous
Jul 10 2017 09:06
@JenteVH The point is on line 1 s1 owns the data returned from String::from("hello"), and let s2 = s1 'moves' the ownership to s2. Since values are not allowed to have multiple owners, s1 cannot be used after the move takes place. Does this clarify?
Jente Van Heuverswyn
@JenteVH
Jul 10 2017 09:11
@jplatte @alteous Makes sense. Thank you
isaacg1
@isaacg1
Jul 10 2017 10:09
What's the current state of profiling for rust? Is there a good tutorial?
isaacg1
@isaacg1
Jul 10 2017 10:11
I saw that one, but I could't get it to work. Specifically, I couldn't get the symbols (names of functions) to work properly.
Thanks though
Jente Van Heuverswyn
@JenteVH
Jul 10 2017 11:29

const grapheme_cat_table
https://github.com/withoutboats/notty/blob/master/src/grapheme_tables.rs

What is going on here?

Daan Rijks
@ijks
Jul 10 2017 11:35
Looking at the code around it, looks like it's a table matching ranges of characters to specific GraphemeCats. Probably has to do with rendering more complicated Unicode characters/codepoints.
So if there is a character c and an entry (a, b, g), if a <= c and c <= b, then the GraphemeCat we need to use is g.
(I have no idea what GraphemeCats are though.)
Oh and the '\u{xxx}' syntax is for specifying Unicode codepoints by their hexadecimal representation, I think.
Jente Van Heuverswyn
@JenteVH
Jul 10 2017 11:40
Is it normal that I don't understand any of this for now or should I give up rust for now and go on with something simpler?
Daan Rijks
@ijks
Jul 10 2017 11:43
Well this code seems highly domain specific, so I would say it's fine if you don't know why it does what it does. Have you read the official book yet?
Jente Van Heuverswyn
@JenteVH
Jul 10 2017 11:43
I'm working my way through it. Had enough of reading for a moment though so thought I'd look through some actual rust projects
Daan Rijks
@ijks
Jul 10 2017 11:45
So is it the const in particular you don't understand, or other things in that file as well?
Arthur
@Biacode
Jul 10 2017 11:45
I would suggest to read Rust book second edition, and then come back and read first edition
@JenteVH find links here
Jente Van Heuverswyn
@JenteVH
Jul 10 2017 11:47
@ijks I can at least read the other parts but once it gets to the array part of the const I have no idea what's going on
('\u{0}', '\u{1f}', GC_Control)
No idea what this is supposed to mean or how it would be used
Your help made it a bit more clear though
@Biacode That's the one I'm going through (2nd edition)
Jonas Platte
@jplatte
Jul 10 2017 11:51
@JenteVH I have one very small Rust project (with only a main module) where I was surprised myself how I found good use for many of Rusts features that don't exist in many other language: https://github.com/jplatte/i3-workspace-scroll. You can read though that and see if you understand what exactly is going on :)
Jente Van Heuverswyn
@JenteVH
Jul 10 2017 11:52
@jplatte I'll check it out, thank you
Daan Rijks
@ijks
Jul 10 2017 11:53
@JenteVH Well basically, it's a really big array of tuples, where the tuples have the type (char, char, GraphemeCat). The \u thingy is for specifying Unicode characters. Are you familiar with Unicode?
Jente Van Heuverswyn
@JenteVH
Jul 10 2017 11:57
@ijks I am. But actually I do more or less understand what's going on now. Like you said, it's a comparison table. The function above it uses a given char and checks which category out of those in the tuples in the constant it belongs to
and a category has a range a..b
given (a,b,g)
Daan Rijks
@ijks
Jul 10 2017 11:59
I think that's what it does, yeah.
Jente Van Heuverswyn
@JenteVH
Jul 10 2017 12:01
Great, thanks
mhsjlw
@mhsjlw
Jul 10 2017 14:15
I want to write a sys library for a library that is statically compiled
Bradley Weston
@bweston92
Jul 10 2017 14:15
@mhsjlw cool :D
mhsjlw
@mhsjlw
Jul 10 2017 14:15
It uses a makefile, and is too complicated to use the gcc lib with it
So what I'm wondering is, is it safe to just call make
Or is there a Rust crate that can help me out with that
Arthur
@Biacode
Jul 10 2017 14:16
you can compile either static and dynamic libraries by using cargo
mhsjlw
@mhsjlw
Jul 10 2017 14:16
Also, where exactly should I store the external source, should I put it in my library, or should it be a git submodule
Or should I get it in my build.rs
I meant, the library that I want to write bindings for is statically compiled
Oops, didn't phrase that quite right :D
(the first time)
Arthur
@Biacode
Jul 10 2017 14:17
ah, okay
mhsjlw
@mhsjlw
Jul 10 2017 14:18
So first I should solve where the source code goes
Is it standard to use a submodule, or is it best if I put the latest release in a vendor folder or something
Arthur
@Biacode
Jul 10 2017 14:19
in terms of architecture I think it is good to have sub module, but idk if it will work...
Robyn Speer
@rspeer
Jul 10 2017 19:42
There are cases where git submodules are the right concept, but you still don't want to actually use them, just because the UX of git submodules is horrible
Rogach
@Rogach
Jul 10 2017 19:50

Hi! I am a newcomer to Rust, so I have a bunch of newcomer questions. Currently I'm struggling with ? and try! - how can I use them efficiently in presence of lots of custom error types? (that don't have a From implementation to convert between each other)
For example, I have type IronResult<T> = Result<T, IronError> from iron crate, and I have Result<T, UrlDecodingError> from urlencoded crate. When I try to use them like this:

router.get("/login", move |req: &mut Request| {
  req.get_ref::<UrlEncodedQuery>()?;
}

Compiler rightfully complains that "the trait std::convert::From<urlencoded::UrlDecodingError> is not implemented for iron::IronError". If I try to implement From<UrlDecodingError> for IronError, I get coherence issues.

Is there a way to avoid lots of manual error matching and handling in this case?

vberset
@vberset
Jul 10 2017 20:22
Hello @Rogach. The error_chain crate is intended for that. It allows to easily generate custom errors and From implementation for external libraries.
Rogach
@Rogach
Jul 10 2017 20:28
@vberset Thanks! Will look at that crate.