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:
2026-10-19 21:17:50 +01:00
co-authored by Claude Opus 5.5
parent e821df15f4
commit 1aee0050a0
24 changed files with 588 additions and 112 deletions
+183 -8
View File
@@ -5,6 +5,40 @@
CXXFLAGS=(-O2 -std=c++17 -Wall -Wextra -DLOCAL -I"$ROOT")
# --debug (or USACO_DEBUG=1): catch out-of-bounds, overflow and UB at runtime
DEBUG_CXXFLAGS=(-g -O1 -fsanitize=address,undefined -fno-omit-frame-pointer -D_GLIBCXX_DEBUG)
RUSTCFLAGS=(-O --edition 2021)
# --debug for rust: debug info, plus the overflow and debug_assert! checks -O drops
DEBUG_RUSTCFLAGS=(-g --edition 2021 -C debug-assertions -C overflow-checks)
# ----- languages -----
# A problem's language is just whichever source file it has: main.cpp or main.rs.
LANGS="cpp rust"
default_lang() { # language 'usaco new' uses when no flag is given
local l="${USACO_LANG:-}"
[[ -z "$l" && -r "$ROOT/.lang" ]] && l="$(<"$ROOT/.lang")"
normalize_lang "${l:-cpp}" 2>/dev/null || echo cpp
}
normalize_lang() { # accepts cpp/c++/cc/rust/rs, echoes cpp|rust
case "${1,,}" in
cpp|c++|cc|cxx) echo cpp ;;
rust|rs) echo rust ;;
*) echo "Error: unknown language '$1' (use cpp or rust)" >&2; return 1 ;;
esac
}
src_name() { [[ "$1" == rust ]] && echo main.rs || echo main.cpp; }
lang_of() { # $1 = problem dir; echoes cpp|rust
if [[ -f "$1/main.rs" ]]; then echo rust
elif [[ -f "$1/main.cpp" ]]; then echo cpp
else echo "Error: no main.cpp or main.rs in $1" >&2; return 1; fi
}
main_file() { # $1 = problem dir; echoes the path of its source file
local l; l="$(lang_of "$1")" || return 1
echo "$1/$(src_name "$l")"
}
find_problem() { # echoes dir of problem in solving/, solved/, or skipped/; unique prefix ok
local name="$1" s d matches=()
@@ -26,13 +60,102 @@ find_problem() { # echoes dir of problem in solving/, solved/, or skipped/; uni
return 1
}
build_problem() { # $1 = problem dir
if [[ -n "${USACO_DEBUG:-}" ]]; then
echo "${DIM}(debug build: sanitizers on)${RST}" >&2
g++ "${CXXFLAGS[@]}" "${DEBUG_CXXFLAGS[@]}" "$1/main.cpp" -o "$1/main"
else
g++ "${CXXFLAGS[@]}" "$1/main.cpp" -o "$1/main"
# ----- rust project model (for rust-analyzer / clippy) -----
# Problems are bare main.rs files with no manifest, and rust-analyzer only does
# real analysis -- type-aware diagnostics, clippy on save -- inside a cargo
# project. So we generate one package at the root that declares every rust
# solution (and template.rs) as its own [[bin]] target. Nothing builds through cargo: 'usaco
# build/run' still shells out to rustc. The manifest exists purely so the editor
# has something to chew on, which is why it's gitignored and regenerated from a
# directory scan (that also prunes problems that moved or went away).
#
# To change which lints fire, edit the [lints.clippy] table below.
sync_cargo_manifest() {
local s d name entry count=0 bins=""
for s in solving solved skipped; do
[[ -d "$ROOT/$s" ]] || continue
for d in "$ROOT/$s"/*/; do
[[ -f "${d}main.rs" ]] || continue
# Target names can't start with a digit, and problems like '101'
# do; the directory prefix keeps them legal and unique.
name="$s-$(basename "$d")"
printf -v entry '[[bin]]\nname = "%s"\npath = "%smain.rs"\n\n' "$name" "${d#"$ROOT"/}"
bins+="$entry"
count=$((count + 1))
done
done
# template.rs is a target too, so mistakes in it surface in the editor and
# in 'usaco lint template' instead of in every problem stamped out from it.
# Its name has no directory prefix, so it can't collide with a problem.
if [[ -f "$ROOT/template.rs" ]]; then
bins+=$'[[bin]]\nname = "template"\npath = "template.rs"\n\n'
fi
if [[ -z "$bins" ]]; then
rm -f "$ROOT/Cargo.toml"
return 0
fi
cat > "$ROOT/Cargo.toml" <<EOF
# GENERATED by 'usaco lint' -- edits here are lost. See sync_cargo_manifest()
# in lib/common.sh. This file only exists so rust-analyzer can analyse the
# solutions and run clippy on them; builds go through rustc, not cargo.
[package]
name = "usaco"
version = "0.0.0"
edition = "2021"
autobins = false
[lints.clippy]
pedantic = { level = "warn", priority = -1 }
# Casts between usize/i32/i64 are the bread and butter of competitive
# programming. Warning on each one buries the lints that find actual bugs.
cast_possible_truncation = "allow"
cast_possible_wrap = "allow"
cast_sign_loss = "allow"
cast_precision_loss = "allow"
# Library-API hygiene, meaningless for a single-file solution.
missing_panics_doc = "allow"
missing_errors_doc = "allow"
must_use_candidate = "allow"
doc_markdown = "allow"
# '|x| x.to_string()' reads better than the spelled-out method path.
redundant_closure_for_method_calls = "allow"
${bins%$'\n'}
EOF
printf '%s\n' "$count"
}
build_problem() { # $1 = problem dir; compiles to $1/main whatever the language
local dir="$1" lang
lang="$(lang_of "$dir")" || return 1
if [[ -n "${USACO_DEBUG:-}" ]]; then
echo "${DIM}(debug build: $([[ $lang == cpp ]] && echo sanitizers || echo overflow checks) on)${RST}" >&2
fi
case "$lang" in
cpp)
if [[ -n "${USACO_DEBUG:-}" ]]; then
g++ "${CXXFLAGS[@]}" "${DEBUG_CXXFLAGS[@]}" "$dir/main.cpp" -o "$dir/main"
else
g++ "${CXXFLAGS[@]}" "$dir/main.cpp" -o "$dir/main"
fi
;;
rust)
# Keep the editor's project model in step with what's on disk.
sync_cargo_manifest >/dev/null
if [[ -n "${USACO_DEBUG:-}" ]]; then
rustc "${DEBUG_RUSTCFLAGS[@]}" "$dir/main.rs" -o "$dir/main"
else
rustc "${RUSTCFLAGS[@]}" "$dir/main.rs" -o "$dir/main"
fi
;;
esac
}
detect_io() { # echoes the io basename (first *.in in $1), or nothing
@@ -60,8 +183,60 @@ solved_at() { # $1 = solved/<name> dir: epoch it (last) entered solved/
[[ -n "$t" ]] && echo "$t" || last_touched "$1"
}
fresh_main() { # $1 = problem dir, $2 = io name: main.cpp from template with STDIO wired up
sed "s/^int main() {$/int main() {\n STDIO(\"$2\");/" "$ROOT/template.cpp" > "$1/main.cpp"
fresh_main() { # $1 = problem dir, $2 = io name, [$3 = language]: source from template, io wired up
local dir="$1" io="$2" lang="${3:-}"
[[ -n "$lang" ]] || lang="$(lang_of "$dir")" || return 1
case "$lang" in
cpp) sed "s/^int main() {$/int main() {\n STDIO(\"$io\");/" "$ROOT/template.cpp" > "$dir/main.cpp" ;;
rust) sed "s/stdio(\"[^\"]*\")/stdio(\"$io\")/" "$ROOT/template.rs" > "$dir/main.rs" ;;
esac
}
wire_io() { # $1 = problem dir, $2 = io name: point the source's file-io call at <io>
local dir="$1" io="$2" lang src
lang="$(lang_of "$dir")" || return 1
src="$dir/$(src_name "$lang")"
if [[ "$lang" == rust ]]; then
if grep -q 'stdio("' "$src"; then
sed -i "s/stdio(\"[^\"]*\")/stdio(\"$io\")/" "$src"
echo "Updated stdio(\"$io\") in main.rs"
else
echo "Warning: couldn't find stdio(\"...\") in main.rs — wire it up manually" >&2
fi
else
if grep -q 'STDIO("' "$src"; then
sed -i "s/STDIO(\"[^\"]*\")/STDIO(\"$io\")/" "$src"
echo "Updated STDIO(\"$io\") in main.cpp"
elif grep -q '^int main() {$' "$src"; then
sed -i "s/^int main() {$/int main() {\n STDIO(\"$io\");/" "$src"
echo "Inserted STDIO(\"$io\") in main.cpp"
else
echo "Warning: couldn't find STDIO(...) or 'int main() {' in main.cpp — wire it up manually" >&2
fi
fi
}
show_io() { # $1 = problem dir: echo the io name the source is wired to, if any
local lang src; lang="$(lang_of "$1")" || return 1
src="$1/$(src_name "$lang")"
if [[ "$lang" == rust ]]; then
grep -o 'stdio("[^"]*")' "$src" | sed 's/stdio("\(.*\)")/\1/' | head -1
else
grep -o 'STDIO("[^"]*")' "$src" | sed 's/STDIO("\(.*\)")/\1/' | head -1
fi
}
run_solution() { # $1 = dir, $2 = io name: run ./main, leave its output in $1/$2.out
# Works for both styles: a solution that opens <io>.in/.out itself (STDIO / stdio)
# and one that just reads stdin and writes stdout (Codeforces-style).
local dir="$1" io="$2" tmp rc=0
rm -f "$dir/$io.out"
tmp="$(mktemp)"
[[ -e "$dir/$io.in" ]] || touch "$dir/$io.in"
(cd "$dir" && ./main) < "$dir/$io.in" > "$tmp" || rc=$?
[[ -e "$dir/$io.out" ]] || cp "$tmp" "$dir/$io.out"
rm -f "$tmp"
return $rc
}
# colors (disabled when not a tty or NO_COLOR is set)