Add Rust support: usaco lang, lint, and main.rs problems
Brings in the stashed Rust work, merged onto the current CLI. A problem's language is whichever source it has (main.cpp or main.rs); 'usaco lang' sets the default for new problems (.lang, or $USACO_LANG), and 'new --rust/--cpp' picks per problem. template.rs is a paste-ready Rust template whose stdio() uses <io>.in/.out when present and stdin/stdout otherwise. run/test now feed <io>.in on stdin and capture stdout when the program doesn't write <io>.out itself, so Codeforces-style solutions work too. 'reset <name> rust|cpp' switches a problem's language (template is still an alias), and --debug builds Rust with overflow checks and debug assertions. 'usaco lint' runs clippy through a generated, gitignored Cargo.toml that also gives rust-analyzer a project; done/skip/undo keep it in step. LeetCode imports stay C++. Also: done stages solved/ and solving/ separately, so a problem that was never committed from solving/ still gets its "Solve" commit, and the site lists main.rs ahead of other files like it does main.cpp. Co-Authored-By: Claude Opus 5.5 <[email protected]>
This commit is contained in:
@@ -7,7 +7,8 @@ site in `site/` (see [site/README.md](site/README.md)).
|
||||
## Layout
|
||||
|
||||
```
|
||||
template.cpp starting template for every problem
|
||||
template.cpp starting template for C++ problems
|
||||
template.rs starting template for Rust problems
|
||||
site/ the website (Go server + Vue SPA), deployed as its own container
|
||||
solving/<name>/ problems in progress
|
||||
solved/<name>/ finished problems
|
||||
@@ -16,17 +17,54 @@ skipped/<name>/ problems set aside for later
|
||||
|
||||
The website's CLI page (`site/web/src/views/CliPage.vue`) is a condensed copy of the Commands section below; keep it in step.
|
||||
|
||||
Each problem dir contains `main.cpp` plus `<io>.in` / `<io>.out` files (USACO file IO is wired up automatically via `STDIO("<io>")`). Optional extras: `.url` (the problem link), `tests/N.in` + `N.out`, `notes.md` (shown on the website, usually written from there), `problem.md` (the statement, saved or fetched from the website's editor) and `explain.md` (an explanation the website had Claude write).
|
||||
Each problem dir contains `main.cpp` *or* `main.rs` plus `<io>.in` / `<io>.out` files. File IO is wired up automatically — `STDIO("<io>")` in C++, `stdio("<io>")` in Rust. A problem's language is simply whichever source file it has, so every command (`run`, `test`, `cat`, `edit`, ...) figures it out on its own. Optional extras: `.url` (the problem link), `tests/N.in` + `N.out`, `notes.md` (shown on the website, usually written from there), `problem.md` (the statement, saved or fetched from the website's editor, or by a LeetCode import) and `explain.md` (an explanation the website had Claude write).
|
||||
|
||||
## Languages
|
||||
|
||||
```
|
||||
./usaco lang # show the default language for new problems
|
||||
./usaco lang rust # set it (stored in .lang)
|
||||
./usaco new foo --rust # or pick per problem (--cpp / --rust)
|
||||
USACO_LANG=cpp ./usaco new foo # or per shell
|
||||
```
|
||||
|
||||
Switch an existing problem's language with `./usaco reset <name> rust` (or `template`) — the old source is kept as a `.bak`. LeetCode imports are always C++.
|
||||
|
||||
**C++** compiles with `g++ -O2 -std=c++17 -Wall -Wextra -DLOCAL`, **Rust** with `rustc -O --edition 2021` (plain `rustc`, no Cargo, so the solution stays a single submittable file). Override the flags via `CXXFLAGS` / `RUSTCFLAGS` in `lib/common.sh`.
|
||||
|
||||
### The Rust template
|
||||
|
||||
`template.rs` is self-contained and paste-ready for any judge. It gives you:
|
||||
|
||||
```rust
|
||||
fn main() {
|
||||
let (mut sc, mut out) = stdio("problem"); // <- usaco new wires the name in
|
||||
let n: usize = sc.next(); // one token, any FromStr type
|
||||
let v: Vec<i64> = sc.vec(n); // n tokens
|
||||
let s: Vec<u8> = sc.bytes(); // a word as bytes
|
||||
writeln!(out, "{}", join(&v, " ")).unwrap();
|
||||
}
|
||||
```
|
||||
|
||||
`stdio(name)` picks its mode from the directory it runs in:
|
||||
|
||||
- `name.in` exists → reads `name.in`, writes `name.out` (USACO, and what `usaco run` / `usaco test` use)
|
||||
- `name.in` missing → reads stdin, writes stdout (paste straight into Codeforces)
|
||||
|
||||
So the same file works locally and on a judge with no edits. Also included: a `Scanner` that slurps the whole input up front and hands out parsed tokens (`next`, `vec`, `pair`, `pairs`, `bytes`, `chars`), `yesno`, `join`, `remax`/`remin`, and the usual `std::collections` imports. `dbg!(x)` goes to stderr, so it never pollutes the `.out` file.
|
||||
|
||||
## Commands
|
||||
|
||||
### Start a problem
|
||||
```
|
||||
./usaco new diamond-collector
|
||||
./usaco new diamond-collector diamond # if the freopen name differs
|
||||
./usaco new diamond-collector diamond # if the file-io name differs
|
||||
./usaco new diamond-collector --url https://usaco.org/index.php?page=viewproblem2&cpid=639
|
||||
```
|
||||
Creates `solving/<name>/main.cpp` from the template and an empty `.in` file, then opens it in `$EDITOR`. `--url` saves the problem link for `open`.
|
||||
```
|
||||
./usaco new diamond-collector --rust # Rust instead of the default
|
||||
```
|
||||
Creates `solving/<name>/main.cpp` (or `main.rs`) from the template and an empty `.in` file, then opens it in `$EDITOR`. `--url` saves the problem link for `open`.
|
||||
|
||||
### Start a LeetCode problem
|
||||
```
|
||||
@@ -49,9 +87,9 @@ Limits: when the statement says "any order", a correct answer in a different ord
|
||||
./usaco run diamond-collector # compile + run, then print the .out file
|
||||
./usaco build diamond-collector # compile only
|
||||
```
|
||||
Compiles with `g++ -O2 -std=c++17 -Wall -Wextra -DLOCAL` and runs inside the problem dir so file IO just works. Put your test input in the `.in` file first.
|
||||
Compiles the problem's source (C++ or Rust) and runs it inside the problem dir, feeding it `<io>.in` on stdin, so both file-IO and stdin/stdout solutions work. Put your test input in the `.in` file first.
|
||||
|
||||
Add `--debug` to `run`, `build` or `test` (or set `USACO_DEBUG=1`) for a sanitizer build (`-fsanitize=address,undefined`, `_GLIBCXX_DEBUG`): out-of-bounds indexing, overflow and other UB crash with a stack trace instead of silently giving wrong answers.
|
||||
Add `--debug` to `run`, `build` or `test` (or set `USACO_DEBUG=1`) for a checked build. C++ gets sanitizers (`-fsanitize=address,undefined`, `_GLIBCXX_DEBUG`): out-of-bounds indexing, overflow and other UB crash with a stack trace instead of silently giving wrong answers. Rust gets `-C overflow-checks -C debug-assertions`, so integer overflow panics instead of wrapping.
|
||||
|
||||
**Debug output:** `LOCAL` is only defined by `./usaco` builds, so the template's `#ifdef LOCAL #include "debugging.h"` (pretty-printers for vectors, maps, pairs…) and its `DBG(x)` macros vanish on the judge — submit `main.cpp` as-is.
|
||||
|
||||
@@ -76,7 +114,7 @@ The website commits too: marking a problem solved/skipped, or editing its link o
|
||||
|
||||
### Edit
|
||||
```
|
||||
./usaco vim diamond-collector # open main.cpp in $EDITOR (defaults to nvim)
|
||||
./usaco vim diamond-collector # open the solution in $EDITOR (defaults to nvim)
|
||||
./usaco e diamond-collector # same thing, shorter (also: edit)
|
||||
```
|
||||
Opens from inside the problem dir, so the `.in`/`.out` files are right there.
|
||||
@@ -90,9 +128,9 @@ Opens from inside the problem dir, so the `.in`/`.out` files are right there.
|
||||
|
||||
### Snippets
|
||||
```
|
||||
./usaco cat diamond-collector # print main.cpp to stdout (pipeable)
|
||||
./usaco cat diamond-collector # print the solution to stdout (pipeable)
|
||||
./usaco cat diam | wl-copy # e.g. pipe it wherever you want
|
||||
./usaco copy diamond-collector # copy main.cpp to the clipboard (alias: cp)
|
||||
./usaco copy diamond-collector # copy the solution to the clipboard (alias: cp)
|
||||
```
|
||||
`copy` auto-detects the clipboard tool: `wl-copy` on Wayland, then `xclip`/`xsel` on X11, and falls back to an OSC52 terminal escape that works even over SSH.
|
||||
|
||||
@@ -116,13 +154,37 @@ The selection persists across sessions in `.current` (cleared automatically if t
|
||||
|
||||
### Misc
|
||||
```
|
||||
./usaco reset diamond-collector # fresh main.cpp from template (old one -> main.cpp.bak), delete binary + .out
|
||||
# asks first (-y skips); keeps tests/ and .in (aliases: template, rm)
|
||||
./usaco io diamond-collector # show the io name; 'io <name> <new>' renames .in/.out and rewires STDIO
|
||||
./usaco clean # delete compiled binaries (main, a.out) and .bak files everywhere
|
||||
./usaco path diamond-collector # print the problem's directory
|
||||
./usaco reset diamond-collector # fresh source from template (old one -> .bak), delete binary + .out
|
||||
# asks first (-y skips); keeps tests/ and .in (aliases: template, rm)
|
||||
./usaco reset diamond-collector rust # ...and switch the problem to Rust
|
||||
./usaco io diamond-collector # show the io name; 'io <name> <new>' renames .in/.out and rewires STDIO/stdio
|
||||
./usaco clean # delete compiled binaries (main, a.out) and .bak files everywhere
|
||||
./usaco lint # clippy over every Rust solution (see "Rust linting" below)
|
||||
./usaco lint excel-sheet # ...or just one
|
||||
./usaco lint template # ...or template.rs itself
|
||||
./usaco path diamond-collector # print the problem's directory
|
||||
```
|
||||
|
||||
### Rust linting
|
||||
|
||||
Problems are bare `main.rs` files, and rust-analyzer only does real analysis
|
||||
(type-aware diagnostics, clippy on save) inside a cargo project. `usaco lint`
|
||||
generates a root `Cargo.toml` that declares every Rust solution, plus
|
||||
`template.rs`, as its own `[[bin]]`, which is enough to give the editor a
|
||||
project to attach to. Any Rust build regenerates it too, so it stays in step as
|
||||
problems are created or moved.
|
||||
|
||||
Nothing compiles through cargo — `usaco build`/`run` still call `rustc`
|
||||
directly. The manifest is purely for tooling, so it's gitignored along with
|
||||
`Cargo.lock` and `target/`; run `usaco lint` once after a fresh clone to
|
||||
recreate it. The lint selection (clippy pedantic, minus the cast and
|
||||
missing-docs noise) lives in `sync_cargo_manifest()` in `lib/common.sh`.
|
||||
|
||||
Editor side: rust-analyzer must actually be installed (`rustup component add
|
||||
rust-analyzer` — the rustup shim is on `$PATH` even when the component isn't,
|
||||
so it fails silently), and `check.command` has to be set to `clippy`, since the
|
||||
default is plain `cargo check`.
|
||||
|
||||
## Shell integration (recommended)
|
||||
|
||||
Add to `~/.zshrc`:
|
||||
@@ -140,8 +202,9 @@ This gives you:
|
||||
./usaco new the-lost-cow --url <link>
|
||||
./usaco open the-lost-cow # read the problem
|
||||
# paste sample input into solving/the-lost-cow/the-lost-cow.in
|
||||
# edit solving/the-lost-cow/main.cpp
|
||||
# edit solving/the-lost-cow/main.rs (or main.cpp)
|
||||
./usaco run the-lost-cow # iterate until the output is right
|
||||
./usaco test create the-lost-cow # lock the sample in as a test case
|
||||
./usaco done the-lost-cow # 🎉 (auto-commits)
|
||||
./usaco sync # publish it on the website
|
||||
```
|
||||
|
||||
Reference in New Issue
Block a user