Where communities thrive


  • Join over 1.5M+ people
  • Join over 100K+ communities
  • Free without limits
  • Create your own community
People
Repo info
Activity
    Benjamin Z
    @benjyz
    have you a lot of experience in C++? I always hated C++ with a passion and never used it
    Thomas Koehler
    @Bastacyclop
    I don't have "a lot of experience" since I'm a student but it was my main language before Rust
    Thomas Koehler
    @Bastacyclop
    personnaly I hate Java a lot more :p
    Alexander Chepurnoy
    @kushti
    @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
    well you can have memory leaks in Java too, so ... :p
    Alexander Chepurnoy
    @kushti
    @Bastacyclop it is usually much harder, but yes, possible
    guys, is it "idiomatic" Rust code? http://dumpz.org/2343306/
    Thomas Koehler
    @Bastacyclop
    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
    @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
    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
    @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
    Don't you want to early return the Err's ?
    Alexander Chepurnoy
    @kushti
    @Bastacyclop it would be nice, how?
    Thomas Koehler
    @Bastacyclop
    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
    yes
    the only thing I dont' like is return in the middle
    Thomas Koehler
    @Bastacyclop
    well try! should help with that
    also you can early return after File::open
    Alexander Chepurnoy
    @kushti
    it is better to have a single expression in Rust also right?
    I'm coming from Scala :)
    Thomas Koehler
    @Bastacyclop
    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
    ah okay
    and about mut, is it okay to have so much of them?
    Thomas Koehler
    @Bastacyclop
    well if you need to mutate things you do it
    Alexander Chepurnoy
    @kushti
    okay
    Thomas Koehler
    @Bastacyclop
    it might seem a bit verbose but it is a lot better to use than the const's of C(++)
    Alexander Chepurnoy
    @kushti
    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
    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
    yes, exactly