Ten short Rust tasks from easy to tricky, each sent on its own, compiled and run against hidden tests. Three takes per question.
What is measured. Ten small functions and types, level 1 to 4, covering lifetimes, generics, closures, iterators, error types, threads and shared ownership. An answer passes only if it compiles and every hidden test passes.
How it runs. Every model gets each question separately with a fresh context, three times. The score is the share of answers that passed.
How it is graded. The code is compiled by rustc and run in an isolated container with no network. No judge reads it. A compile error fails the answer. An empty answer counts as wrong. A question the provider never answered is reported separately and left out of the score.
Write Rust for the 2024 edition on stable, using only the standard library. Answer with one ```rust code block that defines what the question describes. Do not include fn main, tests, examples, explanations, or print statements.
===PART===
Write is_palindrome(s: &str) -> bool. Consider only ASCII letters and digits, ignoring ASCII case and every other character. A string with none of those characters is a palindrome.
===PART===
Write word_count(text: &str) -> HashMap<String, usize>, importing HashMap yourself. Words are separated by any whitespace and compared in lowercase, and the map's keys are lowercase. Punctuation is part of a word.
===PART===
Write longest, which takes two string slices and returns the longer one by str::len without allocating. On a tie, it returns the first. Its result must be usable as long as both inputs are alive.
===PART===
Write parse_pair<T: FromStr>(s: &str, sep: char) -> Option<(T, T)>, importing FromStr yourself. Split s at the first occurrence of sep and parse both sides as T. Return None if sep does not occur or if either side fails to parse. sep may be any Unicode character. parse_pair::<u32>("400x600", 'x') returns Some((400, 600)).
===PART===
Write compose(f, g), generic over the argument and result types. f and are closures implementing . It returns a closure implementing that maps to , and the returned closure must be usable after the function that called has returned.
===PART===
Define , with a constructor , and implement for it. It yields the Fibonacci numbers 0, 1, 1, 2, 3, 5, and so on. It yields every Fibonacci number that fits in a , then from then on. Tests run with overflow checks on.
===PART===
Define deriving , with the variants , , and , and write . The input is a number followed by one unit character, or , and the result is in Celsius, so returns . An empty string gives . If the last character is neither nor , it gives with that character, which may be non-ASCII. A number that does not parse gives . Implement and for , with returning the parse error for , and implement for it.
===PART===
Write , which sums by splitting it across threads from . is a borrowed slice, and may be larger than . It returns 0 for an empty slice and panics when is 0, including when the slice is empty.
===PART===
Define an extension trait that adds a method to every iterator whose items implement . It returns a lazy iterator of consecutive overlapping pairs, so yields then . A source with fewer than two items yields nothing, and an infinite source must work.
===PART===
Define with exactly these fields: , , and , importing those types from the standard library. Write . Write , which also sets the child's parent. Write , the total of this node and all its descendants, and , the number of ancestors, which is 0 for a root.
gFnFnxg(f(x))composeFibFib::new()Iterator<Item = u64>u64Noneenum TempErrorDebugEmptyBadUnit(char)BadNumber(std::num::ParseFloatError)parse_temp(s: &str) -> Result<f64, TempError>CF"212F"100.0EmptyCFBadUnitBadNumberDisplaystd::error::ErrorTempErrorsource()BadNumberFrom<std::num::ParseFloatError>parallel_sum(data: &[u64], workers: usize) -> u64dataworkersstd::threaddataworkersdata.len()workersPairsExt.pairs()Clone(1..4).pairs()(1, 2)(2, 3)TreeNodevalue: i32parent: RefCell<Weak<TreeNode>>children: RefCell<Vec<Rc<TreeNode>>>TreeNode::new(value: i32) -> Rc<TreeNode>TreeNode::add_child(parent: &Rc<TreeNode>, child: Rc<TreeNode>)sum(&self) -> i32depth(&self) -> usize