These are chat archives for rust-lang/rust

4th
Aug 2016
Benjamin Cordes
@benjyz
Aug 04 2016 19:29
how do I put off warnings in cargo?
Thomas Koehler
@Bastacyclop
Aug 04 2016 19:37
which warnings ?
Benjamin Cordes
@benjyz
Aug 04 2016 19:38
"function is never used: xyz, #[warn(dead_code)] on by default"
e.g.
Thomas Koehler
@Bastacyclop
Aug 04 2016 19:40
FYI this comes from the rust compiler (rustc) which is called by cargo. You can use #[allow(dead_code)] in the code. This is called an attribute https://doc.rust-lang.org/book/attributes.html.
Benjamin Cordes
@benjyz
Aug 04 2016 19:43
thanks. equally I try to ignore other warnings with
warning: unused import, #[warn(unused_imports)] on by default
with this.. doesn't work
"#![warn(unused_imports)]"
Thomas Koehler
@Bastacyclop
Aug 04 2016 19:44
#[warn(stuff)] means you want a warning
#[allow(stuff)] means you don't want anything
#[deny(stuff)] means you want an error
wow
Benjamin Cordes
@benjyz
Aug 04 2016 19:44
cool
Thomas Koehler
@Bastacyclop
Aug 04 2016 19:45
Benjamin Cordes
@benjyz
Aug 04 2016 19:47
more basic question: what is a crate?
"The compilation model centers on artifacts called crates. "
Thomas Koehler
@Bastacyclop
Aug 04 2016 19:49
Have you read the Book ?
Benjamin Cordes
@benjyz
Aug 04 2016 19:49
notyet
not yet
Thomas Koehler
@Bastacyclop
Aug 04 2016 19:49
you should :p
Benjamin Cordes
@benjyz
Aug 04 2016 19:50
yes, very well written
the parts I have read so far
any other standard docs? I've listened to some talks recently
Thomas Koehler
@Bastacyclop
Aug 04 2016 19:50
Benjamin Cordes
@benjyz
Aug 04 2016 19:51
talks by Alex Crichton
Thomas Koehler
@Bastacyclop
Aug 04 2016 19:56
I think the Book is the first thing to read, there is a lot of extra stuff in rust-learning
Benjamin Cordes
@benjyz
Aug 04 2016 19:56
does rust compiler use LLVM somewhere?
ok, thx, will go through that
Thomas Koehler
@Bastacyclop
Aug 04 2016 19:56
You also have the Cargo guide
I think it does compile to LLVM which is then compiled to binary but I'm not the best person to ask
^ this is a nice place to get news
@benjyz if you are interested in the compilation process: https://blog.rust-lang.org/2016/04/19/MIR.html
Benjamin Cordes
@benjyz
Aug 04 2016 20:01
ah right, I remember seeing that
MIR.. so much to learn ;)
finally a systems language which isn't C/++
Thomas Koehler
@Bastacyclop
Aug 04 2016 20:02
ahah
I gotta say when I have to go back to C++ it hurts
Benjamin Cordes
@benjyz
Aug 04 2016 20:25
have you a lot of experience in C++? I always hated C++ with a passion and never used it
Thomas Koehler
@Bastacyclop
Aug 04 2016 20:26
I don't have "a lot of experience" since I'm a student but it was my main language before Rust
Thomas Koehler
@Bastacyclop
Aug 04 2016 20:32
personnaly I hate Java a lot more :p
Alexander Chepurnoy
@kushti
Aug 04 2016 21:35
@Bastacyclop probably you havent' experienced a memory leak in pretty big project yet? :)
after searching for a memory leak in C++ in more or less big project people usually going to love Java :)
Thomas Koehler
@Bastacyclop
Aug 04 2016 21:36
well you can have memory leaks in Java too, so ... :p
Alexander Chepurnoy
@kushti
Aug 04 2016 21:37
@Bastacyclop it is usually much harder, but yes, possible
guys, is it "idiomatic" Rust code? http://dumpz.org/2343306/
Thomas Koehler
@Bastacyclop
Aug 04 2016 21:39
if your code is well organised and you take care of what you do with memory it is not so frequent to leak in C++. The worse is when it comes from implicit and sneaky features.
but I have no "industrial" experience
I find C to be much more appealing to debug
(your format! indentation is not right)
Alexander Chepurnoy
@kushti
Aug 04 2016 21:41
@Bastacyclop i dunno much about nowadays, but 10-12 years ago huge C++ projects in our company usually got a garbage collector at some point, sometimes implemented in-house. It is better to use state-of-art Java GCs then
@Bastacyclop thanks!
Thomas Koehler
@Bastacyclop
Aug 04 2016 21:43
line 18
you don't need those brackets
line 11 could be better without too
why do you even return x ?
oh sorry
the = before
feels a bit too much of a one-liner to me
Do you know the try!macro ?
I think you could use it
Alexander Chepurnoy
@kushti
Aug 04 2016 21:47
@Bastacyclop update isn't returning a modified object. It's from "ring" framework. I tried try! but without success :)
thanks, will read
Thomas Koehler
@Bastacyclop
Aug 04 2016 21:52
Don't you want to early return the Err's ?
Alexander Chepurnoy
@kushti
Aug 04 2016 21:53
@Bastacyclop it would be nice, how?
Thomas Koehler
@Bastacyclop
Aug 04 2016 21:54
fn file_digest(digest_alg: &'static digest::Algorithm, file_path: &std::path::Path) -> Result<Digest, std::string::String> {
    std::fs::File::open(file_path).map_err(|why| {
        format!("couldn't open {}: {}", file_path.display(), why.description())
    }).and_then(|mut file| {
        let mut ctx = digest::Context::new(digest_alg);
        let mut chunk = vec![0u8; 128 * 1024];

        loop {
            match file.read(&mut chunk[..]) {
                Ok(0) => break,
                Ok(bytes_read) => ctx.update(&chunk[0..bytes_read]),
                Err(why) => {
                    return Err(format!("couldn't read {}: {}", file_path.display(), why.description()));
                },
            }
        }
        Ok(x.finish())
    })
}
I'm not sure if you can use return inside the closure
I think so
It will probably return from the closure and not from the upper function but I guess it's fine
Alexander Chepurnoy
@kushti
Aug 04 2016 21:56
yes
the only thing I dont' like is return in the middle
Thomas Koehler
@Bastacyclop
Aug 04 2016 21:57
well try! should help with that
also you can early return after File::open
Alexander Chepurnoy
@kushti
Aug 04 2016 21:57
it is better to have a single expression in Rust also right?
I'm coming from Scala :)
Thomas Koehler
@Bastacyclop
Aug 04 2016 21:58
well it is better when it is not worse :p
usually with Result's you do it often
since you abort what you are doing
Alexander Chepurnoy
@kushti
Aug 04 2016 21:58
ah okay
and about mut, is it okay to have so much of them?
Thomas Koehler
@Bastacyclop
Aug 04 2016 22:00
well if you need to mutate things you do it
Alexander Chepurnoy
@kushti
Aug 04 2016 22:01
okay
Thomas Koehler
@Bastacyclop
Aug 04 2016 22:01
it might seem a bit verbose but it is a lot better to use than the const's of C(++)
Alexander Chepurnoy
@kushti
Aug 04 2016 22:03
it's not much verbose, but in Scala such an amount of mutables is a red flag. Rust is different. Where can I read "idiomatic" Rust code snippets?
Thomas Koehler
@Bastacyclop
Aug 04 2016 22:03
I think you can replace all you Err handling with try!, remember that try!uses Into/From automatically.
because Scala is a lot more functionnal right ?
Alexander Chepurnoy
@kushti
Aug 04 2016 22:04
yes, exactly
Thomas Koehler
@Bastacyclop
Aug 04 2016 22:04
Rust is not
Alexander Chepurnoy
@kushti
Aug 04 2016 22:04
I see
Thomas Koehler
@Bastacyclop
Aug 04 2016 22:04
Well you have a lot of functionnal-like stuff
but it is still a system language right
about code snippets
hmm
the Book gives a good impression of course
there is this ongoing thing: http://aturon.github.io/
otherwise I don't really know I guess
usually I just try to make things look right
Alexander Chepurnoy
@kushti
Aug 04 2016 22:08
thanks!
Thomas Koehler
@Bastacyclop
Aug 04 2016 22:35
fn file_digest(digest_alg: &'static digest::Algorithm, file_path: &std::path::Path) -> Result<Digest, std::string::String> {
    let mut file = try!(File::open(file_path).map_err(|why| format!("couldn't open {}: {}", file_path.display(), why.description()));
    let mut ctx = digest::Context::new(digest_alg);
    let mut chunk = vec![0u8; 128 * 1024];

    loop {
        let bytes_read = try!(file.read(&mut chunk[..]).map_err(|why| format!("couldn't read {}: {}", file_path.display(), why.description())));
        if bytes_read > 0 {
            ctx.update(&chunk[0..bytes_read]);
        } else {
            break;
        }
    }

    Ok(ctx.finish())
}
@kushti like this maybe ?
don't forget you can shorten things like std::path::Path and std::io::File with use's
also I don't think that returning a String error is very idiomatic
Thomas Koehler
@Bastacyclop
Aug 04 2016 22:53

Actually if what you want is a buffered reader:

use std::path::Path;
use std::io;

// do you really need this lifetime to be 'static ??
fn file_digest(digest_alg: &'static digest::Algorithm, file_path: &Path) -> io::Result<Digest> {
    let mut file = try!(io::File::open(file_path));
    let mut reader = BufReader::with_capacity(capacity, file);
    let mut ctx = digest::Context::new(digest_alg);
    for byte in reader.bytes() {
        ctx.update(try!(byte));
    }
    Ok(ctx.finish())
}

Also, your digest stuff could directly take an Iterator over bytes ?

If you want you can still format your Strings from the Err afterwards
Thomas Koehler
@Bastacyclop
Aug 04 2016 23:00
my final version (hoping I have not completly diverged from what your code was about xD) would be something like:
use std::path::Path;
use std::io;

fn file_digest(digest_alg: &digest::Algorithm, file_path: &Path) -> io::Result<Digest> {
    let mut file = try!(io::File::open(file_path));
    let mut reader = io::BufReader::with_capacity(capacity, file);
    try!(bytes_digest(digest_alg, reader.bytes()))
}