Giter Club home page Giter Club logo

Comments (15)

budziq avatar budziq commented on September 24, 2024
  • cat /proc/cpuinfo or cat /proc/vmstat
  • ls /tmp
  • df
  • groups

from rust-cookbook.

dtolnay avatar dtolnay commented on September 24, 2024

By non-contrived I mean things that you couldn't just do in ordinary Rust. So I would prefer not to show cat /proc/cpuinfo or ls /tmp because you would just write those using the standard library.

df isn't the best for an example because it would need a lot of code to parse and do anything useful with the output.

groups could be a good one! I think the libc crate has a function to list groups but unclear whether that is more or less complicated than parsing the command output.

from rust-cookbook.

budziq avatar budziq commented on September 24, 2024

oh now I get it. How about these then:

  • uname -a
  • lsblk
  • history
  • mount
  • journalctl

something simple yet not easily doable with std

from rust-cookbook.

Michael-F-Bryan avatar Michael-F-Bryan commented on September 24, 2024

Do we want to make sure the command used is commonly available on both Windows and unix systems? If so that probably limits this to commands like git and rustc -Z because I doubt most Windows people would have GNU Binutils (nm) installed.

from rust-cookbook.

dtolnay avatar dtolnay commented on September 24, 2024

Yes good call, that would be ideal.

from rust-cookbook.

Michael-F-Bryan avatar Michael-F-Bryan commented on September 24, 2024

Would it be acceptable to pull in the regex crate as a dependency? Regex is pretty much the swiss army knife of text manipulation, so I imagine the cookbook will include several uses of it anyway.

from rust-cookbook.

dtolnay avatar dtolnay commented on September 24, 2024

Yes that would be fine! What do you have in mind?

from rust-cookbook.

Michael-F-Bryan avatar Michael-F-Bryan commented on September 24, 2024

Off the top of my head, parsing something like git log --oneline into a commit hash, some optional tags or branch names, and its message?

61d6ac0 (HEAD -> master) Added a collapsible sidebar
2318e2b Added chart.js and a Chart component
4d0fb2a Started setting up the component tree
06fa097 Initial ReactJS app skeleton

I'm happy to make a PR for this one when I get home.

from rust-cookbook.

dtolnay avatar dtolnay commented on September 24, 2024

Okay that seems reasonable to me.

If someone wanted to get commit hashes and messages would you feel comfortable recommending this shell command + regex approach over using the git2 crate?

from rust-cookbook.

Michael-F-Bryan avatar Michael-F-Bryan commented on September 24, 2024

For something reasonably simple like this I'd recommend parsing the process's output like we're doing here. Anything more complex though and it's probably going to be easier to use the library.

As it is, this should just be a couple lines of filter_map() and map() corresponding to what you'd usually do in bash, so pulling in git2 is probably overkill.

from rust-cookbook.

dtolnay avatar dtolnay commented on September 24, 2024

Makes sense! I look forward to the PR.

from rust-cookbook.

Michael-F-Bryan avatar Michael-F-Bryan commented on September 24, 2024

I've made an initial implementation (gist). @dtolnay, any chance you can give it a look over and see if it can be improved?

In particular, the error handling on line 32 (where I'm creating a new io::Error) feels a little odd. I was thinking of implementing my own error type or using error_chain!{...} but that's adding a lot of boilerplate to an already lengthy example.

from rust-cookbook.

budziq avatar budziq commented on September 24, 2024

@Michael-F-Bryan This looks great!.
I would have only one small nit:
Please see our error handling section section. Using error_chain! is the promoted error handling way (please do not worry about the noise we are working on it https://github.com/brson/rust-cookbook/pull/107).
Also could you update the example to the form

fn run() -> Result<()> {
//actual main
    Ok(())
}

quick_main!(run);

The rationale is neatly explained in https://github.com/brson/rust-cookbook/pull/53#issuecomment-300244571

from rust-cookbook.

Michael-F-Bryan avatar Michael-F-Bryan commented on September 24, 2024

Cheers for the links, I've updated the gist and added error_chain to make error handling nicer/more idiomatic.

Do you think that intermediate Commit type is still necessary now that I've put everything into a single run() function? I could just use a tuple, but that loses a bit of the readability gained by having named fields.

Also, which section would this belong in? I was thinking of adding it to the bottom of the "basics" section, but if we end up adding examples like piping one subprocess's output into another or running a child process in the background and doing other things while you wait for it to exit, it may be worth creating a new section in the cookbook. Thoughts?

from rust-cookbook.

budziq avatar budziq commented on September 24, 2024

The gist looks excellent, please create a PR.

I would add the example to the "basics" section for now. Once we have more examples we will think about how these should be split.

from rust-cookbook.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.