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:
+133
@@ -0,0 +1,133 @@
|
||||
#![allow(dead_code, unused_imports, unused_macros, unused_mut, unused_variables)]
|
||||
use std::cmp::{max, min, Ordering, Reverse};
|
||||
use std::collections::{BTreeMap, BTreeSet, BinaryHeap, HashMap, HashSet, VecDeque};
|
||||
use std::fs::File;
|
||||
use std::io::{self, BufWriter, Read, Write};
|
||||
|
||||
// ----- TYPES -----
|
||||
type Ll = i64;
|
||||
type Pii = (i32, i32);
|
||||
|
||||
// ----- FILE IO -----
|
||||
// stdio("name") picks the mode from the directory it runs in:
|
||||
// name.in exists -> read name.in, write name.out (USACO + `usaco run/test`)
|
||||
// name.in missing -> read stdin, write stdout (Codeforces submission)
|
||||
// So the same file works locally and when pasted into a judge.
|
||||
fn stdio(name: &str) -> (Scanner, Box<dyn Write>) {
|
||||
let infile = format!("{name}.in");
|
||||
if std::path::Path::new(&infile).exists() {
|
||||
let src = std::fs::read_to_string(&infile).expect("could not read .in file");
|
||||
let out = File::create(format!("{name}.out")).expect("could not create .out file");
|
||||
(Scanner::new(&src), Box::new(BufWriter::new(out)))
|
||||
} else {
|
||||
let mut src = String::new();
|
||||
io::stdin()
|
||||
.read_to_string(&mut src)
|
||||
.expect("could not read stdin");
|
||||
(Scanner::new(&src), Box::new(BufWriter::new(io::stdout())))
|
||||
}
|
||||
}
|
||||
|
||||
// ----- INPUT -----
|
||||
// Slurps the whole input, then hands out whitespace-separated tokens.
|
||||
// let n: usize = sc.next(); one token
|
||||
// let (a, b) = (sc.next::<i64>(), sc.next::<i64>());
|
||||
// let v: Vec<i64> = sc.vec(n); n tokens
|
||||
// let s: Vec<u8> = sc.bytes(); a word as bytes (s[i] - b'a')
|
||||
struct Scanner {
|
||||
toks: std::str::SplitAsciiWhitespace<'static>,
|
||||
}
|
||||
|
||||
impl Scanner {
|
||||
fn new(src: &str) -> Scanner {
|
||||
let leaked: &'static str = Box::leak(src.to_owned().into_boxed_str());
|
||||
Scanner {
|
||||
toks: leaked.split_ascii_whitespace(),
|
||||
}
|
||||
}
|
||||
fn next<T: std::str::FromStr>(&mut self) -> T {
|
||||
let tok = self.toks.next().expect("ran out of input");
|
||||
tok.parse().ok().expect("could not parse token")
|
||||
}
|
||||
fn vec<T: std::str::FromStr>(&mut self, n: usize) -> Vec<T> {
|
||||
(0..n).map(|_| self.next()).collect()
|
||||
}
|
||||
fn pair<T: std::str::FromStr, U: std::str::FromStr>(&mut self) -> (T, U) {
|
||||
(self.next(), self.next())
|
||||
}
|
||||
fn pairs<T: std::str::FromStr, U: std::str::FromStr>(&mut self, n: usize) -> Vec<(T, U)> {
|
||||
(0..n).map(|_| self.pair()).collect()
|
||||
}
|
||||
fn bytes(&mut self) -> Vec<u8> {
|
||||
self.next::<String>().into_bytes()
|
||||
}
|
||||
fn chars(&mut self) -> Vec<char> {
|
||||
self.next::<String>().chars().collect()
|
||||
}
|
||||
fn leet_vec<T: std::str::FromStr>(&mut self) -> Vec<T> {
|
||||
let s: String = self.next();
|
||||
s.trim_matches(|c| c == '[' || c == ']')
|
||||
.split(',')
|
||||
.filter(|s| !s.is_empty())
|
||||
.map(|s| s.parse().ok().expect("leet vec parse err"))
|
||||
.collect()
|
||||
}
|
||||
fn leet_grid<T: std::str::FromStr>(&mut self) -> Vec<Vec<T>> {
|
||||
let s: String = self.next();
|
||||
let s = s.trim_start_matches('[').trim_end_matches(']');
|
||||
let mut res = Vec::new();
|
||||
for row in s.split("],[") {
|
||||
let cur = row
|
||||
.split(',')
|
||||
.filter(|c| !c.is_empty())
|
||||
.map(|c| c.parse().ok().expect("leet grid error"))
|
||||
.collect();
|
||||
res.push(cur);
|
||||
}
|
||||
res
|
||||
}
|
||||
}
|
||||
|
||||
// ----- OUTPUT -----
|
||||
// Write with writeln!(out, "{}", x). These help with the common shapes:
|
||||
fn yesno(b: bool) -> &'static str {
|
||||
if b {
|
||||
"YES"
|
||||
} else {
|
||||
"NO"
|
||||
}
|
||||
}
|
||||
// join(&v, " ") -> "1 2 3"
|
||||
fn join<T: std::fmt::Display>(v: &[T], sep: &str) -> String {
|
||||
v.iter()
|
||||
.map(|x| x.to_string())
|
||||
.collect::<Vec<_>>()
|
||||
.join(sep)
|
||||
}
|
||||
|
||||
// ----- DEBUG -----
|
||||
// dbg!(x) is built in and goes to stderr, so it never pollutes the .out file.
|
||||
|
||||
// ----- MAX MIN -----
|
||||
fn remax<T: PartialOrd>(a: &mut T, b: T) -> bool {
|
||||
if b > *a {
|
||||
*a = b;
|
||||
true
|
||||
} else {
|
||||
false
|
||||
}
|
||||
}
|
||||
fn remin<T: PartialOrd>(a: &mut T, b: T) -> bool {
|
||||
if b < *a {
|
||||
*a = b;
|
||||
true
|
||||
} else {
|
||||
false
|
||||
}
|
||||
}
|
||||
|
||||
fn main() {
|
||||
let (mut sc, mut out) = stdio("problem");
|
||||
let t: usize = sc.next();
|
||||
for _ in 0..t {}
|
||||
}
|
||||
Reference in New Issue
Block a user