Rust Programming

8438 readers
26 users here now

founded 5 years ago
MODERATORS
376
377
378
379
380
381
 
 
382
383
384
385
386
 
 

Here is a simplified example of my problem:

struct User;
struct Community;

enum Actor {
    User(User),
    Community(Community),
}

trait Name {
    fn name() -> &'static str;
}

impl Name for User {
    fn name() -> &'static str { "/u/nutomic" }
}

impl Name for Community {
    fn name() -> &'static str { "/c/rust " }
}

fn main() {
    let actor = Actor::Community(Community);
    println!("{}", actor.name());
}

Playground link

As you may have noticed, this doesnt compile. The only solution I can think of is with a match. I want to avoid that because I have an enum with 30+ variants, and a trait with multiple methods. So that would be a huge match statement in each method, when the compiler should know that the enum variants all have the same trait.

So, do you know any way to make this work without match?

387
388
 
 

Hi Rustaceans, I am a C++ developer looking for some answers from the world of Rust. I am basically trying to implement loose coupling of sw components in a project where dynamic polymorphism is not allowed for performance reasons. To me this sounds very rust-ish.

My primary reason for doing this is to be able to mock and stub the component interfaces for automatic testing, and normally I would use abstract base classes for interfaces and inject the components into each other as depencencies.

So to the question... is it possible using basic language features to create two structs that both implement traits that are used by the other, inject the structs as dependencies to each other and also be able to stub or mock these structs?

389
390
391
392
4
submitted 3 years ago* (last edited 3 years ago) by [email protected] to c/[email protected]
 
 

Summary of the issues related to the Rust trademark policy with regards to GNU/Linux distributions specifically.

393
394
395
 
 

Looks like the general release is planned for June 18!

396
13
submitted 3 years ago* (last edited 3 years ago) by [email protected] to c/[email protected]
 
 

Hi, first of all sorry if it's not the place to ask (I'll delete the post if it's the case).

I started to learn Rust a few days ago and I'm currently doing the rustlings course. I'm at the exercise error_handling/errors2.rs. I've succeed to make it compile using the ? operator which I find kinda easy to understand.

My problem is the following: It's stated in the exercise that there's another solution. After looking at the hint, I know that the other solution is the use of match. I've kept looking at the docs, the examples and also StackOverflow posts but I can't make it work since I really don't understand how it's supposed to work.

Here's what I tried:

pub fn total_cost(item_quantity: &str) -> Result<i32, ParseIntError> {
         let processing_fee = 1;
         let cost_per_item = 5;
         let qty = item_quantity.parse();

         match qty {
                 Ok(qty) => qty * cost_per_item + processing_fee,
                 Err(e) => Err(e),                                           
         };
}

And here's what the compiler give:expected enum 'Result<i32, ParseIntError>' found unit type '()'

397
398
 
 

Does anyone have any experience with this? Is Rust mature enough to be able to do this?

399
 
 

Features

  • Written in memory safe Rust
  • Amazingly fast - due to using more or less advanced algorithms and multithreading
  • Free, Open Source without ads
  • Multiplatform - works on Linux, Windows and macOS
  • Cache support - second and further scans should be a lot of faster than the first
  • CLI frontend - for easy automation
  • GUI frontend - uses modern GTK 3 and looks similar to FSlint
  • Rich search option - allows setting absolute included and excluded directories, set of allowed file extensions or excluded items with the * wildcard
  • Multiple tools to use:
    • Duplicates - Finds duplicates basing on file name, size, hash, first 1 MB of hash
    • Empty Folders - Finds empty folders with the help of an advanced algorithm
    • Big Files - Finds the provided number of the biggest files in given location
    • Empty Files - Looks for empty files across the drive
    • Temporary Files - Finds temporary files
    • Similar Images - Finds images which are not exactly the same (different resolution, watermarks)
    • Zeroed Files - Finds files which are filled with zeros (usually corrupted)
    • Same Music - Searches for music with same artist, album etc.
    • Invalid Symbolic Links - Shows symbolic links which points to non-existent files/directories
    • Broken Files - Finds files with an invalid extension or that are corrupted
400
view more: ‹ prev next ›