These are chat archives for rust-lang/rust

1st
Oct 2017
Jente Van Heuverswyn
@JenteVH
Oct 01 2017 10:49
let mut g = "test";
println!("{}", g);
let mut g = "test";
println!(g);
First one works, second one doesn’t. Could someone explain me the details of this?
“Expected a literal”. However, g is a literal right?
Andrey Lesnikov
@ozkriff
Oct 01 2017 10:50
nope, g is a variable
Jente Van Heuverswyn
@JenteVH
Oct 01 2017 10:51
Which contains a literal (I’m a js guy. Probably shows :-) )
Doesn’t println!(g) mean println!(“test”) in the end?
TatriX
@TatriX
Oct 01 2017 11:04
You may think of g as a thing which points to the literal somewhere in the executable. And println want an actual "literal".
Jente Van Heuverswyn
@JenteVH
Oct 01 2017 11:06
Does this work the same way in different languages? I can’t recall I ever had such a ‘problem'
TatriX
@TatriX
Oct 01 2017 11:07
println! is a macro. There is no macro in js.
Check this out to see how macro expands before the compiler can actually compile the code
Jente Van Heuverswyn
@JenteVH
Oct 01 2017 11:09
Oh I see. Guess I just didn’t get that far yet
Would you say it’s a good idea to just take these things for granted for now?
TatriX
@TatriX
Oct 01 2017 11:10
Yeah. println! ensures in the compile time that you give it enough args
Jente Van Heuverswyn
@JenteVH
Oct 01 2017 11:11
Alright, thank you
Robert Peters
@r2p2
Oct 01 2017 12:32
fn f2(i: &mut i32) {
    *i *= 2
}

fn f1(i: &mut i32) {
    f2(i)             // &mut forbidden
}

fn main() {
    let mut i = 10;
    f1(&mut i);       // &mut required
    println!("{}", i);
}
Could someone enlighten me on my two comments? If I write f2(&mut i) I get the error message: cannot borrow mutably but if I omit &mut it does technically the same its just not verbose. But the verbosity is required in the f1 call. I just don't know why there is a difference.
Aleksey Kladov
@matklad
Oct 01 2017 12:34
Hm, inside of f1 the type of i is &mut i32, inside main the type of i is lust i32.
Robert Peters
@r2p2
Oct 01 2017 12:39
So &mut in front of a variable name is some kind of cast into &mut <type>; I thought it is only used to make it clear that a mutual reference is passed. In that case, in f2 it would try to pass a mutual reference to a mutual reference if &mut is added and that’s what the compiler is complaining about?
Steve Klabnik
@steveklabnik
Oct 01 2017 12:39
it's not a cast, it creates a new value
in main, i has the type i32. f1 takes &mut i32. So, you use &mut i to create a new &mut i32 that refers to i
in f1, i has the type &mut i32, so when you pass it to f2, it already has the correct type
shoulda used more backticks tehre
there

you could have also written somethig like

let mut i = 10;
let j = &mut i;
f1(j);

does that make sense?

now, j is a variable rather than a temporary
Robert Peters
@r2p2
Oct 01 2017 12:43
I see, I see. I think that makes sense...
Thank you, I thought of it the wrong way.
Steve Klabnik
@steveklabnik
Oct 01 2017 12:47
glad it makes sense now :)
Jente Van Heuverswyn
@JenteVH
Oct 01 2017 13:41
Does rust have a shorthand for if?
Steve Klabnik
@steveklabnik
Oct 01 2017 13:42
if is already pretty short ;)
it's two letters!
more seriously, if you're asking about :?, the ternary operator, no, rust doesn't have it. if is idiomatic
Jente Van Heuverswyn
@JenteVH
Oct 01 2017 13:43
Idiomatic in which sense? ?: feels pretty natural to me. I suppose you mean in regards to convention?
Steve Klabnik
@steveklabnik
Oct 01 2017 13:43
yes
Jonas Platte
@jplatte
Oct 01 2017 13:43
@JenteVH if is an expression rather than a statement in Rust
Steve Klabnik
@steveklabnik
Oct 01 2017 13:43
let x = if something { 5 } then { 6 };
Jonas Platte
@jplatte
Oct 01 2017 13:44
Many other languages basically have :? because if isn't an expresion
Steve Klabnik
@steveklabnik
Oct 01 2017 13:44
rather than `let x = if something : 5 ? 6;
Jonas Platte
@jplatte
Oct 01 2017 13:44
AFAIK
Steve Klabnik
@steveklabnik
Oct 01 2017 13:44
`
@jplatte yup
Jente Van Heuverswyn
@JenteVH
Oct 01 2017 13:44
So is there no one liner to write the following?
if end == 0 {
   word = &s1[start..];
} else {
    word = &s1[start..end];
}
Steve Klabnik
@steveklabnik
Oct 01 2017 13:44
ther eis
Jonas Platte
@jplatte
Oct 01 2017 13:44
word = if end == 0 { &s1[start..] } else { &s1[start..end] };
Steve Klabnik
@steveklabnik
Oct 01 2017 13:45
let word = if end == 0 { &s1[start..] } else { &s1[start..end] }
well yeah, i guess you wouldn't use let , but usually when i see code like the aboe it has a let word; above
Jonas Platte
@jplatte
Oct 01 2017 13:45
Yeah. You do need a semicolon at the end though ;)
Jente Van Heuverswyn
@JenteVH
Oct 01 2017 13:45
But doesn’t this make it pretty unreadable? Does to me atleast
Steve Klabnik
@steveklabnik
Oct 01 2017 13:45
it's not any more readable or not than it would be with the :?
i would probably put this on two lines
or else, re-write it
if end == 0 {
    end = s1.len();
}
word = &s1[start..end];
Jente Van Heuverswyn
@JenteVH
Oct 01 2017 13:47

word = end == 0 ? &s1[start..] : &s1[start..end];

This is more readable to me atleast. Maybe just cause I’m used to it

Steve Klabnik
@steveklabnik
Oct 01 2017 13:47
quite possibly :)
Jente Van Heuverswyn
@JenteVH
Oct 01 2017 13:48
Alright, thanks in any case :-)
Steve Klabnik
@steveklabnik
Oct 01 2017 13:48
no problem :)
Jente Van Heuverswyn
@JenteVH
Oct 01 2017 14:06
ugh more questions
How to add a default value in a struct?
Jente Van Heuverswyn
@JenteVH
Oct 01 2017 14:11
@ozkriff Where is the actual value in this?
impl Default for Kind {
    fn default() -> Kind { Kind::A }
}
Andrey Lesnikov
@ozkriff
Oct 01 2017 14:12
Kind::A in this case
Steve Klabnik
@steveklabnik
Oct 01 2017 14:12
@JenteVH do you want to add a default for one field, or for the whole struct?
Jente Van Heuverswyn
@JenteVH
Oct 01 2017 14:12
For one field
Steve Klabnik
@steveklabnik
Oct 01 2017 14:13
so, if that type implements default, when creating it, you'd use Default::default
but there's no direct way to say "when i make this struct, let me leave this field out and use that for the value"
Jente Van Heuverswyn
@JenteVH
Oct 01 2017 14:14
struct User {
    Name: String = String::from(“Ben”),
}
Basically I just want this as a result
as an example
Steve Klabnik
@steveklabnik
Oct 01 2017 14:15
you'd do that by writing a method on User
    fn with_default_name() -> User {
        User {
            name: String::from("ben"),
        }
    }

let user = User::with_defeault_name();
Jente Van Heuverswyn
@JenteVH
Oct 01 2017 14:17
I see how that works. Is that actually the best way to do this though? Seems like a lot of code for not much. Inline in the struct obviously doesn’t work. I was reading somewhere that they are thinking of implementing this though?
Steve Klabnik
@steveklabnik
Oct 01 2017 14:18
i'm not aware of any plans
rust is, in many ways, a minimal language
TatriX
@TatriX
Oct 01 2017 14:18
    let options = SomeOptions { foo: 42, ..Default::default() };
Steve Klabnik
@steveklabnik
Oct 01 2017 14:19
@TatriX
that doesn't do what @JenteVH wants though
that would give a name of ""
because Default for String is ""
Jente Van Heuverswyn
@JenteVH
Oct 01 2017 14:19
Well, just to give an ‘actual’ example
TatriX
@TatriX
Oct 01 2017 14:20
Sure, one need to implement a custom default to override empty values
Jente Van Heuverswyn
@JenteVH
Oct 01 2017 14:20
struct User {
    username: Option<String>,
    email: Option<String>,
    sign_in_count: u64,
}

Above, from the book.

Sign_in_count in the beginning would always be 0 for a new user, right? So I’d default this to 0 instead of having to explicitly say it’s 0

Steve Klabnik
@steveklabnik
Oct 01 2017 14:20
@TatriX then every field needs a default
which is not always appropriate
Jente Van Heuverswyn
@JenteVH
Oct 01 2017 14:24
So basically, right now, not possible without a function that you call when assigning to variable. Correct?
Steve Klabnik
@steveklabnik
Oct 01 2017 14:24
yeah; you'd either write some sort of constructor-like function, or, in extreme cases, the builder pattern
but there's no built-in syntax to do this
Jente Van Heuverswyn
@JenteVH
Oct 01 2017 14:24
Alright. Thank you
Jonas Platte
@jplatte
Oct 01 2017 14:38
@JenteVH What you'd do in a case like that is make a new function:
impl User {
    fn new(username: Option<String>, email: Option<String>) -> User {
        User { username, email, sign_in_count: 0 }
    }
}
Jente Van Heuverswyn
@JenteVH
Oct 01 2017 14:42
@jplatte Was trying that out now as suggested by @steveklabnik.
unreadable
@unreadable
Oct 01 2017 14:42
how do people here distribute rust binaries? From what I see, the target folder seems to have a bunch of files comparing to go binaries..
Steve Klabnik
@steveklabnik
Oct 01 2017 14:42
@krypton97 by default, rust code is statically linked, only libc is not
if you include C libraries, you may have to use options to statically link it, depending on its -sys crate
unreadable
@unreadable
Oct 01 2017 14:43
do I have to ship my binary with libc?
Steve Klabnik
@steveklabnik
Oct 01 2017 14:43
and, if you want 100% static linking, you can use musl
instead of glibc
unreadable
@unreadable
Oct 01 2017 14:43
Right, I've seen linux binaries shipped with musl
Steve Klabnik
@steveklabnik
Oct 01 2017 14:43
usually, if you want glibc, you compile on an old centos box, since libcs are compatible, you just need to get it as old as you want
unreadable
@unreadable
Oct 01 2017 14:43
are there any disadvantages using musl over libc?
Steve Klabnik
@steveklabnik
Oct 01 2017 14:45
i haven't used it enough to give you a good answer there
unreadable
@unreadable
Oct 01 2017 14:45
well, I found out what I needed, thanks!
Steve Klabnik
@steveklabnik
Oct 01 2017 14:45
no problem! :)
Jente Van Heuverswyn
@JenteVH
Oct 01 2017 14:50
struct User {
    pub username: Option<String>,
    pub email: Option<String>,
    pub sign_in_count: u64,
}

With an optional username as above, is the code below an appropriate way of getting the actual value from username?

println!("{}", user.username.unwrap());

Without unwrap I’m getting errors
unreadable
@unreadable
Oct 01 2017 14:50
I'm suing unwrap() as well
using*
CrLF0710
@crlf0710
Oct 01 2017 14:52
Using {:?} is easier... if you just want to see the value...
The unwrap() way works, however will panic if it is None.
Jente Van Heuverswyn
@JenteVH
Oct 01 2017 14:54
What’s that do exactly? the :? part
CrLF0710
@crlf0710
Oct 01 2017 14:54
{} uses the Display trait to output, and {:?} uses the Debug trait.
Jente Van Heuverswyn
@JenteVH
Oct 01 2017 14:55
I see
CrLF0710
@crlf0710
Oct 01 2017 14:56
Option<T> hasn't got a default Display implementation, because you'll likely need to make up your own rules of output if you really have to "display" it. there's a default Debug implementation though.
Jente Van Heuverswyn
@JenteVH
Oct 01 2017 14:58
I’m getting Some("test”) as a result now though.
CrLF0710
@crlf0710
Oct 01 2017 14:58
And "None" when the value is None.
Jente Van Heuverswyn
@JenteVH
Oct 01 2017 14:58
Working as intended then?
CrLF0710
@crlf0710
Oct 01 2017 14:59
yeah, Debug trait outputs this way.
Jente Van Heuverswyn
@JenteVH
Oct 01 2017 15:00
So if I get this straight, with an optional parameter you still have to define None as a parameter instead of just leaving it out?
CrLF0710
@crlf0710
Oct 01 2017 15:02
Well, it depends on how you want to deal with the None case.
Jente Van Heuverswyn
@JenteVH
Oct 01 2017 15:03
Well I’m writing a contructor for a struct. Basically I just want the ‘Some’ value, or if no value is provided it should not be set
CrLF0710
@crlf0710
Oct 01 2017 15:05
if None is not a valid value, do not use Option<T>, use a single constructor to collect all the arguments at once, or maybe you can use the builder pattern if you want to collect the arguments one by one.
Jente Van Heuverswyn
@JenteVH
Oct 01 2017 15:07
Aha, the builder pattern does look like what I need
I should really just read the whole book before I start asking questions
CrLF0710
@crlf0710
Oct 01 2017 15:09
:smile:
Jente Van Heuverswyn
@JenteVH
Oct 01 2017 15:09
Thanks for the effort either way
Jason Ozias
@CraZySacX
Oct 01 2017 17:19
Can anyone explain what std::convert::Infallible is in nightly?

I have to add

impl From<Infallible> for Error {
    fn from(infallible: Infallible) -> Self {
        match infallible {}
    }
}

to my error_chain Error now.

Ashley Mannix
@KodrAus
Oct 01 2017 17:27
@CraZySacX I think it's an implementation detail in TryFrom so we can describe conversions that can't fail? I would open an issue on rust-lang. I don't think you should have to add that impl in your code
Jason Ozias
@CraZySacX
Oct 01 2017 17:28
Ok, will do. Seems to follow. The error was triggered on a TryFrom converting a u32 to an isize.
unreadable
@unreadable
Oct 01 2017 19:41
are there any alternatives for cargo?
Fra ns
@snarf95_twitter
Oct 01 2017 19:41
Is it possible to blit a pixel array directly to screen or inside a window? Without using OpenGL
Zakarum
@omni-viral
Oct 01 2017 19:42
@snarf95_twitter Do you want to know how to call graphics driver directly?
Fra ns
@snarf95_twitter
Oct 01 2017 19:43
Well preferably not..
I guess something more high level for generating pixels on CPU and displaying them
Zakarum
@omni-viral
Oct 01 2017 19:44
@snarf95_twitter if your windowing library supports it
you can add some pixels to the window
Jonas Platte
@jplatte
Oct 01 2017 19:45
@snarf95_twitter for outputting directly to the screen, look up "framebuffer"
if that is really what you want
but using the framebuffer directly only makes sense when you have full control over what is displayed.
you wouldn't use it to draw stuff when you already have other programs outputting graphics
(AFAIK, haven't done it myself yet)
Fra ns
@snarf95_twitter
Oct 01 2017 19:59
Hmm SDL2 has the most high level api I’ve seen so far so probably gonna pick that
Denis Lisov
@tanriol
Oct 01 2017 20:01
@krypton97 Partially. What exactly do you want the alternative to do?
unreadable
@unreadable
Oct 01 2017 20:02
I still got the error "create not found", but I think it's a vscode bug
and also missing features like yarn add and so on..but fixed it with cargo-edit
Fra ns
@snarf95_twitter
Oct 01 2017 20:10
Use cargo init to create a new project.
unreadable
@unreadable
Oct 01 2017 20:16
Won't help, whenever I try to install an extern crate I get the not found issue. I'm a newbie with rust, but it's just a package manager, idk what's going wrong with my code... too bad I got used with node's package managers
Denis Lisov
@tanriol
Oct 01 2017 20:17
How do you add the extern crate?
Fra ns
@snarf95_twitter
Oct 01 2017 20:17
Hmm I haven’t had any problems with cargo..
unreadable
@unreadable
Oct 01 2017 20:19
Hmm, vscode 1 - 0. I looked through the target filed and ran the binary in the terminal and worked as expected. What IDE are u guys using?
Fra ns
@snarf95_twitter
Oct 01 2017 20:22
Well vscode works really well for me
unreadable
@unreadable
Oct 01 2017 20:24
linux/mac/win?
Horace He
@Chillee
Oct 01 2017 20:26
I also use vscode, although I haven't done rust on any large projects
I'm running arch @krypton97
Denis Lisov
@tanriol
Oct 01 2017 20:27
I'm not using an IDE, just a good editor.
unreadable
@unreadable
Oct 01 2017 20:28
I'm on solus, should not matter. I'm thinking on switching back to the good old sublime
Horace He
@Chillee
Oct 01 2017 20:28
IDE vs text editors is kind of a bizarre distinction nowadays
Fra ns
@snarf95_twitter
Oct 01 2017 20:28
Windoge
& mac