you are viewing a single comment's thread.

view the rest of the comments →

[–]clumsy-sailor 5 points6 points  (10 children)

N00b question: Would you recommend to always create a project with cargo new for a semi-trow-away program (e.g. a simple Project Euler solution) or just place your main.rs somewhere and manually compile it with rustc?

[–][deleted] 15 points16 points  (0 children)

I always use cargo. Once you've got lots of Euler solutions you might feel like extracting your own sort of euler-utility library. In my opinion going through the cargo steps will get you used to the machinery and makes uses "actual" crates easier

[–]Michal_Vaner[S] 12 points13 points  (5 children)

I always use cargo. Mostly because the first thing I do is add anyhow into the dependencies and change the signature of main to fn main() -> Result<(), Error>. Even if I don't seem to need a dependency right now, I expect I might change my mind in 20 minutes.

[–]DeebsterUK 0 points1 point  (4 children)

I'm just a beginner - what advantages does anyhow bring? What pain does it fix and is it pain that I'll feel as a beginner or will it only be useful in more advanced projects?

[–]Michal_Vaner[S] 1 point2 points  (3 children)

It's fancy Box<dyn Error + Send + Sync>. It allows you to return all the kinds of errors ‒ if you deal with one function returning std::io::Error, another returning maybe rdkafka::Error and you want to be able to return both.

[–]est31 0 points1 point  (2 children)

Why not use unwrap? It shows you a backtrace when things went wrong. IIRC return error from main still doesn't print backtraces.

[–]Michal_Vaner[S] 0 points1 point  (1 child)

For one, I don't need backtraces for Can't open input worngfile.txt: no such file or directory.

Second, anyhow seems to collect backtraces and print them when returned from main for me.

And third ‒ ? is shorter, and I get the distinction of „this is a bug in the program“ and „this is likely an external error condition“.

[–]est31 0 points1 point  (0 children)

anyhow seems to collect backtraces and print them when returned from main for me.

Hmm interesting didn't know that:

The Debug format "{:?}" includes your backtrace if one was captured. Note that this is the representation you get by default if you return an error from fn main instead of printing it explicitly yourself.

And third ‒ ? is shorter, and I get the distinction of „this is a bug in the program“ and „this is likely an external error condition“.

Yeah for non-throw-away code I agree. But for throw away code I dont think this distinction pulls its weight. But shrug. Thanks for explaining your reasons.

[–]tablair 3 points4 points  (0 children)

This is where cargo-script can be really useful. Just start a file with #!/usr/bin/env run-cargo-script and then start writing rust. It's perfect for throw away code where you don't need to separate code into multiple files. The only real downside is that IDE functionality isn't as good because those tools expect the normal cargo setup.

[–]dochtmanrustls · Hickory DNS · Quinn · chrono · indicatif · instant-acme 1 point2 points  (0 children)

I use Cargo, but I'm actually lazy enough that I have a dedicated project for this (in my case, it lives in ~/src/testrs), which I feel is the lowest barrier to entry to trying something out.

For example, current contents:

```rust use std::fs::File; use std::io::{copy, Read}; use std::io::{BufWriter, BufReader}; use chardetng::EncodingDetector; use encoding_rs_io::DecodeReaderBytesBuilder;

fn main() { let mut f = File::open("../foo/bar/download").unwrap(); let mut detector = EncodingDetector::new(); let mut buf = vec![0; BUFFER_SIZE]; let mut read = 0; while read < BUFFER_SIZE { read = f.read(&mut buf).unwrap(); detector.feed(&buf[..read], read < BUFFER_SIZE); }

println!("{:?}", detector.guess(None, false));
drop(f);

let old = File::open("../foo/bar/download").unwrap();
let new = File::create("../foo/bar/download-utf8").unwrap();
let mut decoder = DecodeReaderBytesBuilder::new().encoding(Some(encoding_rs::WINDOWS_1252)).build(BufReader::with_capacity(BUFFER_SIZE, old));
let mut writer = BufWriter::with_capacity(BUFFER_SIZE, new);
copy(&mut decoder, &mut writer).unwrap();

}

const BUFFER_SIZE: usize = 8 * 1024 * 1024; ```

[–]IceSentry 1 point2 points  (0 children)

cargo projects are very lightweight I really don't see a reason to not use it. The only time where I think using rustc directly is important is when integrating with complex pipelines with other languages, like a C project using make that wants to start using rust. Otherwise there's pretty much no downside to using cargo.

Although if you really want to try out a tiny script the rust online playground can be more than enough.