Where communities thrive


  • Join over 1.5M+ people
  • Join over 100K+ communities
  • Free without limits
  • Create your own community
People
Repo info
Activity
    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
    Thomas Koehler
    @Bastacyclop
    Rust is not
    Alexander Chepurnoy
    @kushti
    I see
    Thomas Koehler
    @Bastacyclop
    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
    thanks!
    Thomas Koehler
    @Bastacyclop
    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

    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
    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()))
    }
    Alexander Chepurnoy
    @kushti
    @Bastacyclop impressive changes :) will try them, thanks!
    Benjamin Z
    @benjyz
    @kushti what does this code do? ;)
    Andréy Lesnikóv
    @ozkriff
    Zakarum
    @omni-viral
    What for Future have both successful result and error result in 'futures-rs' and in 'eventual-rs'?
    Couldn't this be implemented by using Result<T, E> for single result type of Future?
    Andrew Duffy
    @a10y
    Is this the right place to ask a noob question or is there another group for that :)
    Zakarum
    @omni-viral
    It's ok for any question. If it is related to Rust )
    Daniel Collin
    @emoon
    Is there a way to do a file.read_to_string(&mut foo) that takes in a size instead of reading the whole file? I can do some own implementation of it of course but wonder if there is an existing way to do it already?
    Johann Tuffe
    @tafia
    You should use read_exact and then convert it to a String
    Daniel Collin
    @emoon
    Yeah, I guess I need to do it myself at least.
    Fletcher Haynes
    @fhaynes
    Hello! Is there a way to split a String on just the first occurrence of a delimiter?
    Lilian Anatolie Moraru
    @lilianmoraru
    Fletcher Haynes
    @fhaynes
    ahh, splitn is what I was looking for. Thank you, I somehow missed that in the docs
    loggerhead
    @loggerhead
    Hello. Is there anyone used rust-crypto to encrypt and decrypt stream data?
    Seems SynchronousStreamCipher is what I want, but there is only a method named "process" to do encrypt and I can't find the method which do decrypt
    Sergey Noskov
    @Albibek
    @loggerhead , are you sure you're about correct semantics here? It seems to me that this trait is more about stream ciphers(https://en.wikipedia.org/wiki/Stream_cipher), but not about encrypting stream data
    Anyways since stream ciphers are using xor, the decrypt operation is repeating of encrypt operation on encrypted data.
    loggerhead
    @loggerhead
    I have read the wiki, seems "SynchronousStreamCipher::process" is used to encrypt stream data. Otherwise, what's function it provide?
    Oh, thank you very much! I have been stuck at finding a way to decrypt stream data because of my ignorant of the working principle.
    Ironpillow
    @ironpillow
    hi all, I am working on a concurrent ping program (send ping to multiple hosts at the same time). I got the basic - send/receive icmp packets for now. I am trying to make this async using mioco lib. But being new to concurrent programming, I am stuck. https://gist.github.com/ironpillow/d64f644fc42a05c2bcb5e4a3dff84815
    Andréy Lesnikóv
    @ozkriff
    Andrey Ivanov
    @a-nigredo

    hi all, I'm using manual(https://doc.rust-lang.org/book) for studying rust but I'm stuck on https://doc.rust-lang.org/book/crates-and-modules.html#exporting-a-public-interface. It doesnt compile with message:

    
       Compiling phrases v0.1.0 (file:///D:/rust/phrases)
    src\main.rs:7:38: 7:54 error: unresolved name `greetings::hello` [E0425]
    src\main.rs:7     println!("Hello in English: {}", greetings::hello());
                                                       ^~~~~~~~~~~~~~~~
    <std macros>:2:27: 2:58 note: in this expansion of format_args!
    <std macros>:3:1: 3:54 note: in this expansion of print! (defined in <std macros>)
    src\main.rs:7:5: 7:58 note: in this expansion of println! (defined in <std macros>)
    src\main.rs:7:38: 7:54 help: run `rustc --explain E0425` to see a detailed explanation
    src\main.rs:8:40: 8:58 error: unresolved name `farewells::goodbye` [E0425]
    src\main.rs:8     println!("Goodbye in English: {}", farewells::goodbye());
                                                         ^~~~~~~~~~~~~~~~~~
    <std macros>:2:27: 2:58 note: in this expansion of format_args!
    <std macros>:3:1: 3:54 note: in this expansion of print! (defined in <std macros>)
    src\main.rs:8:5: 8:62 note: in this expansion of println! (defined in <std macros>)
    src\main.rs:8:40: 8:58 help: run `rustc --explain E0425` to see a detailed explanation
    error: aborting due to 2 previous errors
    error: Could not compile `phrases`.

    is it an error in manual or I'm doing something wrong?

    Lilian Anatolie Moraru
    @lilianmoraru
    You probably copied the text without the previous statements
    You can press on the website(go with the mouse over the code in the website) on the "Run" button. It will open up the playground with all the code that is needed to compile
    For demonstration purposes the code is cut in smaller pieces, so if you take a small piece, it won't compile by itself.
    If the modules are internal, than you are probably missing
    mod greetings;
    mod farewells;
    Andrey Ivanov
    @a-nigredo
    @lilianmoraru ok, ty, I'll try