repo
stringlengths
6
65
file_url
stringlengths
81
311
file_path
stringlengths
6
227
content
stringlengths
0
32.8k
language
stringclasses
1 value
license
stringclasses
7 values
commit_sha
stringlengths
40
40
retrieved_at
stringdate
2026-01-04 15:31:58
2026-01-04 20:25:31
truncated
bool
2 classes
rust-lang/rustlings
https://github.com/rust-lang/rustlings/blob/7850a73d95c02840f4ab3bf8d9571b08410e5467/solutions/05_vecs/vecs1.rs
solutions/05_vecs/vecs1.rs
fn array_and_vec() -> ([i32; 4], Vec<i32>) { let a = [10, 20, 30, 40]; // Array // Used the `vec!` macro. let v = vec![10, 20, 30, 40]; (a, v) } fn main() { // You can optionally experiment here. } #[cfg(test)] mod tests { use super::*; #[test] fn test_array_and_vec_similarity() { ...
rust
MIT
7850a73d95c02840f4ab3bf8d9571b08410e5467
2026-01-04T15:31:58.719144Z
false
rust-lang/rustlings
https://github.com/rust-lang/rustlings/blob/7850a73d95c02840f4ab3bf8d9571b08410e5467/solutions/05_vecs/vecs2.rs
solutions/05_vecs/vecs2.rs
fn vec_loop(input: &[i32]) -> Vec<i32> { let mut output = Vec::new(); for element in input { output.push(2 * element); } output } fn main() { // You can optionally experiment here. } #[cfg(test)] mod tests { use super::*; #[test] fn test_vec_loop() { let input = [2, ...
rust
MIT
7850a73d95c02840f4ab3bf8d9571b08410e5467
2026-01-04T15:31:58.719144Z
false
rust-lang/rustlings
https://github.com/rust-lang/rustlings/blob/7850a73d95c02840f4ab3bf8d9571b08410e5467/solutions/14_generics/generics2.rs
solutions/14_generics/generics2.rs
struct Wrapper<T> { value: T, } impl<T> Wrapper<T> { fn new(value: T) -> Self { Wrapper { value } } } fn main() { // You can optionally experiment here. } #[cfg(test)] mod tests { use super::*; #[test] fn store_u32_in_wrapper() { assert_eq!(Wrapper::new(42).value, 42); ...
rust
MIT
7850a73d95c02840f4ab3bf8d9571b08410e5467
2026-01-04T15:31:58.719144Z
false
rust-lang/rustlings
https://github.com/rust-lang/rustlings/blob/7850a73d95c02840f4ab3bf8d9571b08410e5467/solutions/14_generics/generics1.rs
solutions/14_generics/generics1.rs
// `Vec<T>` is generic over the type `T`. In most cases, the compiler is able to // infer `T`, for example after pushing a value with a concrete type to the vector. // But in this exercise, the compiler needs some help through a type annotation. fn main() { // `u8` and `i8` can both be converted to `i16`. let ...
rust
MIT
7850a73d95c02840f4ab3bf8d9571b08410e5467
2026-01-04T15:31:58.719144Z
false
rust-lang/rustlings
https://github.com/rust-lang/rustlings/blob/7850a73d95c02840f4ab3bf8d9571b08410e5467/solutions/10_modules/modules1.rs
solutions/10_modules/modules1.rs
mod sausage_factory { fn get_secret_recipe() -> String { String::from("Ginger") } // Added `pub` before `fn` to make the function accessible outside the module. pub fn make_sausage() { get_secret_recipe(); println!("sausage!"); } } fn main() { sausage_factory::make_saus...
rust
MIT
7850a73d95c02840f4ab3bf8d9571b08410e5467
2026-01-04T15:31:58.719144Z
false
rust-lang/rustlings
https://github.com/rust-lang/rustlings/blob/7850a73d95c02840f4ab3bf8d9571b08410e5467/solutions/10_modules/modules3.rs
solutions/10_modules/modules3.rs
use std::time::{SystemTime, UNIX_EPOCH}; fn main() { match SystemTime::now().duration_since(UNIX_EPOCH) { Ok(n) => println!("1970-01-01 00:00:00 UTC was {} seconds ago!", n.as_secs()), Err(_) => panic!("SystemTime before UNIX EPOCH!"), } }
rust
MIT
7850a73d95c02840f4ab3bf8d9571b08410e5467
2026-01-04T15:31:58.719144Z
false
rust-lang/rustlings
https://github.com/rust-lang/rustlings/blob/7850a73d95c02840f4ab3bf8d9571b08410e5467/solutions/10_modules/modules2.rs
solutions/10_modules/modules2.rs
mod delicious_snacks { // Added `pub` and used the expected alias after `as`. pub use self::fruits::PEAR as fruit; pub use self::veggies::CUCUMBER as veggie; mod fruits { pub const PEAR: &str = "Pear"; pub const APPLE: &str = "Apple"; } mod veggies { pub const CUCUMBER:...
rust
MIT
7850a73d95c02840f4ab3bf8d9571b08410e5467
2026-01-04T15:31:58.719144Z
false
rust-lang/rustlings
https://github.com/rust-lang/rustlings/blob/7850a73d95c02840f4ab3bf8d9571b08410e5467/solutions/quizzes/quiz2.rs
solutions/quizzes/quiz2.rs
// Let's build a little machine in the form of a function. As input, we're going // to give a list of strings and commands. These commands determine what action // is going to be applied to the string. It can either be: // - Uppercase the string // - Trim the string // - Append "bar" to the string a specified amount of...
rust
MIT
7850a73d95c02840f4ab3bf8d9571b08410e5467
2026-01-04T15:31:58.719144Z
false
rust-lang/rustlings
https://github.com/rust-lang/rustlings/blob/7850a73d95c02840f4ab3bf8d9571b08410e5467/solutions/quizzes/quiz3.rs
solutions/quizzes/quiz3.rs
// An imaginary magical school has a new report card generation system written // in Rust! Currently, the system only supports creating report cards where the // student's grade is represented numerically (e.g. 1.0 -> 5.5). However, the // school also issues alphabetical grades (A+ -> F-) and needs to be able to // pri...
rust
MIT
7850a73d95c02840f4ab3bf8d9571b08410e5467
2026-01-04T15:31:58.719144Z
false
rust-lang/rustlings
https://github.com/rust-lang/rustlings/blob/7850a73d95c02840f4ab3bf8d9571b08410e5467/solutions/quizzes/quiz1.rs
solutions/quizzes/quiz1.rs
// Mary is buying apples. The price of an apple is calculated as follows: // - An apple costs 2 rustbucks. // - However, if Mary buys more than 40 apples, the price of each apple in the // entire order is reduced to only 1 rustbuck! fn calculate_price_of_apples(n_apples: u64) -> u64 { if n_apples > 40 { n_...
rust
MIT
7850a73d95c02840f4ab3bf8d9571b08410e5467
2026-01-04T15:31:58.719144Z
false
rust-lang/rustlings
https://github.com/rust-lang/rustlings/blob/7850a73d95c02840f4ab3bf8d9571b08410e5467/solutions/16_lifetimes/lifetimes1.rs
solutions/16_lifetimes/lifetimes1.rs
// The Rust compiler needs to know how to check whether supplied references are // valid, so that it can let the programmer know if a reference is at risk of // going out of scope before it is used. Remember, references are borrows and do // not own their own data. What if their owner goes out of scope? fn longest<'a>...
rust
MIT
7850a73d95c02840f4ab3bf8d9571b08410e5467
2026-01-04T15:31:58.719144Z
false
rust-lang/rustlings
https://github.com/rust-lang/rustlings/blob/7850a73d95c02840f4ab3bf8d9571b08410e5467/solutions/16_lifetimes/lifetimes3.rs
solutions/16_lifetimes/lifetimes3.rs
// Lifetimes are also needed when structs hold references. struct Book<'a> { // ^^^^ added a lifetime annotation author: &'a str, // ^^ title: &'a str, // ^^ } fn main() { let book = Book { author: "George Orwell", title: "1984", }; println!("{} by {}", ...
rust
MIT
7850a73d95c02840f4ab3bf8d9571b08410e5467
2026-01-04T15:31:58.719144Z
false
rust-lang/rustlings
https://github.com/rust-lang/rustlings/blob/7850a73d95c02840f4ab3bf8d9571b08410e5467/solutions/16_lifetimes/lifetimes2.rs
solutions/16_lifetimes/lifetimes2.rs
fn longest<'a>(x: &'a str, y: &'a str) -> &'a str { if x.len() > y.len() { x } else { y } } fn main() { let string1 = String::from("long string is long"); // Solution 1: You can move `strings2` out of the inner block so that it is // not dropped before the print statement. let string2 = String::fro...
rust
MIT
7850a73d95c02840f4ab3bf8d9571b08410e5467
2026-01-04T15:31:58.719144Z
false
rust-lang/rustlings
https://github.com/rust-lang/rustlings/blob/7850a73d95c02840f4ab3bf8d9571b08410e5467/solutions/00_intro/intro1.rs
solutions/00_intro/intro1.rs
fn main() { // Congratulations, you finished the first exercise 🎉 // As an introduction to Rustlings, the first exercise only required // entering `n` in the terminal to go to the next exercise. }
rust
MIT
7850a73d95c02840f4ab3bf8d9571b08410e5467
2026-01-04T15:31:58.719144Z
false
rust-lang/rustlings
https://github.com/rust-lang/rustlings/blob/7850a73d95c02840f4ab3bf8d9571b08410e5467/solutions/00_intro/intro2.rs
solutions/00_intro/intro2.rs
fn main() { // `println!` instead of `printline!`. println!("Hello world!"); }
rust
MIT
7850a73d95c02840f4ab3bf8d9571b08410e5467
2026-01-04T15:31:58.719144Z
false
rust-lang/rustlings
https://github.com/rust-lang/rustlings/blob/7850a73d95c02840f4ab3bf8d9571b08410e5467/solutions/21_macros/macros4.rs
solutions/21_macros/macros4.rs
// Added semicolons to separate the macro arms. #[rustfmt::skip] macro_rules! my_macro { () => { println!("Check out my macro!"); }; ($val:expr) => { println!("Look at this other macro: {}", $val); }; } fn main() { my_macro!(); my_macro!(7777); }
rust
MIT
7850a73d95c02840f4ab3bf8d9571b08410e5467
2026-01-04T15:31:58.719144Z
false
rust-lang/rustlings
https://github.com/rust-lang/rustlings/blob/7850a73d95c02840f4ab3bf8d9571b08410e5467/solutions/21_macros/macros2.rs
solutions/21_macros/macros2.rs
// Moved the macro definition to be before its call. macro_rules! my_macro { () => { println!("Check out my macro!"); }; } fn main() { my_macro!(); }
rust
MIT
7850a73d95c02840f4ab3bf8d9571b08410e5467
2026-01-04T15:31:58.719144Z
false
rust-lang/rustlings
https://github.com/rust-lang/rustlings/blob/7850a73d95c02840f4ab3bf8d9571b08410e5467/solutions/21_macros/macros1.rs
solutions/21_macros/macros1.rs
macro_rules! my_macro { () => { println!("Check out my macro!"); }; } fn main() { my_macro!(); // ^ }
rust
MIT
7850a73d95c02840f4ab3bf8d9571b08410e5467
2026-01-04T15:31:58.719144Z
false
rust-lang/rustlings
https://github.com/rust-lang/rustlings/blob/7850a73d95c02840f4ab3bf8d9571b08410e5467/solutions/21_macros/macros3.rs
solutions/21_macros/macros3.rs
// Added the `macro_use` attribute. #[macro_use] mod macros { macro_rules! my_macro { () => { println!("Check out my macro!"); }; } } fn main() { my_macro!(); }
rust
MIT
7850a73d95c02840f4ab3bf8d9571b08410e5467
2026-01-04T15:31:58.719144Z
false
rust-lang/rustlings
https://github.com/rust-lang/rustlings/blob/7850a73d95c02840f4ab3bf8d9571b08410e5467/solutions/01_variables/variables5.rs
solutions/01_variables/variables5.rs
fn main() { let number = "T-H-R-E-E"; println!("Spell a number: {number}"); // Using variable shadowing // https://doc.rust-lang.org/book/ch03-01-variables-and-mutability.html#shadowing let number = 3; println!("Number plus two is: {}", number + 2); }
rust
MIT
7850a73d95c02840f4ab3bf8d9571b08410e5467
2026-01-04T15:31:58.719144Z
false
rust-lang/rustlings
https://github.com/rust-lang/rustlings/blob/7850a73d95c02840f4ab3bf8d9571b08410e5467/solutions/01_variables/variables1.rs
solutions/01_variables/variables1.rs
fn main() { // Declaring variables requires the `let` keyword. let x = 5; println!("x has the value {x}"); }
rust
MIT
7850a73d95c02840f4ab3bf8d9571b08410e5467
2026-01-04T15:31:58.719144Z
false
rust-lang/rustlings
https://github.com/rust-lang/rustlings/blob/7850a73d95c02840f4ab3bf8d9571b08410e5467/solutions/01_variables/variables2.rs
solutions/01_variables/variables2.rs
fn main() { // The easiest way to fix the compiler error is to initialize the // variable `x`. By setting its value to an integer, Rust infers its type // as `i32` which is the default type for integers. let x = 42; // But we can enforce a type different from the default `i32` by adding // a ty...
rust
MIT
7850a73d95c02840f4ab3bf8d9571b08410e5467
2026-01-04T15:31:58.719144Z
false
rust-lang/rustlings
https://github.com/rust-lang/rustlings/blob/7850a73d95c02840f4ab3bf8d9571b08410e5467/solutions/01_variables/variables6.rs
solutions/01_variables/variables6.rs
// The type of constants must always be annotated. const NUMBER: u64 = 3; fn main() { println!("Number: {NUMBER}"); }
rust
MIT
7850a73d95c02840f4ab3bf8d9571b08410e5467
2026-01-04T15:31:58.719144Z
false
rust-lang/rustlings
https://github.com/rust-lang/rustlings/blob/7850a73d95c02840f4ab3bf8d9571b08410e5467/solutions/01_variables/variables3.rs
solutions/01_variables/variables3.rs
#![allow(clippy::needless_late_init)] fn main() { // Reading uninitialized variables isn't allowed in Rust! // Therefore, we need to assign a value first. let x: i32 = 42; println!("Number {x}"); // It is possible to declare a variable and initialize it later. // But it can't be used before i...
rust
MIT
7850a73d95c02840f4ab3bf8d9571b08410e5467
2026-01-04T15:31:58.719144Z
false
rust-lang/rustlings
https://github.com/rust-lang/rustlings/blob/7850a73d95c02840f4ab3bf8d9571b08410e5467/solutions/01_variables/variables4.rs
solutions/01_variables/variables4.rs
fn main() { // In Rust, variables are immutable by default. // Adding the `mut` keyword after `let` makes the declared variable mutable. let mut x = 3; println!("Number {x}"); x = 5; println!("Number {x}"); }
rust
MIT
7850a73d95c02840f4ab3bf8d9571b08410e5467
2026-01-04T15:31:58.719144Z
false
rust-lang/rustlings
https://github.com/rust-lang/rustlings/blob/7850a73d95c02840f4ab3bf8d9571b08410e5467/solutions/04_primitive_types/primitive_types3.rs
solutions/04_primitive_types/primitive_types3.rs
fn main() { // An array with 100 elements of the value 42. let a = [42; 100]; if a.len() >= 100 { println!("Wow, that's a big array!"); } else { println!("Meh, I eat arrays like that for breakfast."); panic!("Array not big enough, more elements needed"); } }
rust
MIT
7850a73d95c02840f4ab3bf8d9571b08410e5467
2026-01-04T15:31:58.719144Z
false
rust-lang/rustlings
https://github.com/rust-lang/rustlings/blob/7850a73d95c02840f4ab3bf8d9571b08410e5467/solutions/04_primitive_types/primitive_types1.rs
solutions/04_primitive_types/primitive_types1.rs
fn main() { let is_morning = true; if is_morning { println!("Good morning!"); } let is_evening = !is_morning; if is_evening { println!("Good evening!"); } }
rust
MIT
7850a73d95c02840f4ab3bf8d9571b08410e5467
2026-01-04T15:31:58.719144Z
false
rust-lang/rustlings
https://github.com/rust-lang/rustlings/blob/7850a73d95c02840f4ab3bf8d9571b08410e5467/solutions/04_primitive_types/primitive_types6.rs
solutions/04_primitive_types/primitive_types6.rs
fn main() { // You can optionally experiment here. } #[cfg(test)] mod tests { #[test] fn indexing_tuple() { let numbers = (1, 2, 3); // Tuple indexing syntax. let second = numbers.1; assert_eq!(second, 2, "This is not the 2nd number in the tuple!"); } }
rust
MIT
7850a73d95c02840f4ab3bf8d9571b08410e5467
2026-01-04T15:31:58.719144Z
false
rust-lang/rustlings
https://github.com/rust-lang/rustlings/blob/7850a73d95c02840f4ab3bf8d9571b08410e5467/solutions/04_primitive_types/primitive_types2.rs
solutions/04_primitive_types/primitive_types2.rs
fn main() { let my_first_initial = 'C'; if my_first_initial.is_alphabetic() { println!("Alphabetical!"); } else if my_first_initial.is_numeric() { println!("Numerical!"); } else { println!("Neither alphabetic nor numeric!"); } // Example with an emoji. let your_chara...
rust
MIT
7850a73d95c02840f4ab3bf8d9571b08410e5467
2026-01-04T15:31:58.719144Z
false
rust-lang/rustlings
https://github.com/rust-lang/rustlings/blob/7850a73d95c02840f4ab3bf8d9571b08410e5467/solutions/04_primitive_types/primitive_types4.rs
solutions/04_primitive_types/primitive_types4.rs
fn main() { // You can optionally experiment here. } #[cfg(test)] mod tests { #[test] fn slice_out_of_array() { let a = [1, 2, 3, 4, 5]; // 0 1 2 3 4 <- indices // ------- // | // +--- slice // Note that the upper ...
rust
MIT
7850a73d95c02840f4ab3bf8d9571b08410e5467
2026-01-04T15:31:58.719144Z
false
rust-lang/rustlings
https://github.com/rust-lang/rustlings/blob/7850a73d95c02840f4ab3bf8d9571b08410e5467/solutions/04_primitive_types/primitive_types5.rs
solutions/04_primitive_types/primitive_types5.rs
fn main() { let cat = ("Furry McFurson", 3.5); // Destructuring the tuple. let (name, age) = cat; println!("{name} is {age} years old"); }
rust
MIT
7850a73d95c02840f4ab3bf8d9571b08410e5467
2026-01-04T15:31:58.719144Z
false
rust-lang/rustlings
https://github.com/rust-lang/rustlings/blob/7850a73d95c02840f4ab3bf8d9571b08410e5467/solutions/02_functions/functions5.rs
solutions/02_functions/functions5.rs
fn square(num: i32) -> i32 { // Removed the semicolon `;` at the end of the line below to implicitly return the result. num * num } fn main() { let answer = square(3); println!("The square of 3 is {answer}"); }
rust
MIT
7850a73d95c02840f4ab3bf8d9571b08410e5467
2026-01-04T15:31:58.719144Z
false
rust-lang/rustlings
https://github.com/rust-lang/rustlings/blob/7850a73d95c02840f4ab3bf8d9571b08410e5467/solutions/02_functions/functions2.rs
solutions/02_functions/functions2.rs
// The type of function arguments must be annotated. // Added the type annotation `u64`. fn call_me(num: u64) { for i in 0..num { println!("Ring! Call number {}", i + 1); } } fn main() { call_me(3); }
rust
MIT
7850a73d95c02840f4ab3bf8d9571b08410e5467
2026-01-04T15:31:58.719144Z
false
rust-lang/rustlings
https://github.com/rust-lang/rustlings/blob/7850a73d95c02840f4ab3bf8d9571b08410e5467/solutions/02_functions/functions4.rs
solutions/02_functions/functions4.rs
fn is_even(num: i64) -> bool { num % 2 == 0 } // The return type must always be annotated. fn sale_price(price: i64) -> i64 { if is_even(price) { price - 10 } else { price - 3 } } fn main() { let original_price = 51; println!("Your sale price is {}", sale_price(original_price))...
rust
MIT
7850a73d95c02840f4ab3bf8d9571b08410e5467
2026-01-04T15:31:58.719144Z
false
rust-lang/rustlings
https://github.com/rust-lang/rustlings/blob/7850a73d95c02840f4ab3bf8d9571b08410e5467/solutions/02_functions/functions1.rs
solutions/02_functions/functions1.rs
// Some function with the name `call_me` without arguments or a return value. fn call_me() { println!("Hello world!"); } fn main() { call_me(); }
rust
MIT
7850a73d95c02840f4ab3bf8d9571b08410e5467
2026-01-04T15:31:58.719144Z
false
rust-lang/rustlings
https://github.com/rust-lang/rustlings/blob/7850a73d95c02840f4ab3bf8d9571b08410e5467/solutions/02_functions/functions3.rs
solutions/02_functions/functions3.rs
fn call_me(num: u8) { for i in 0..num { println!("Ring! Call number {}", i + 1); } } fn main() { // `call_me` expects an argument. call_me(5); }
rust
MIT
7850a73d95c02840f4ab3bf8d9571b08410e5467
2026-01-04T15:31:58.719144Z
false
rust-lang/rustlings
https://github.com/rust-lang/rustlings/blob/7850a73d95c02840f4ab3bf8d9571b08410e5467/solutions/12_options/options3.rs
solutions/12_options/options3.rs
#[derive(Debug)] struct Point { x: i32, y: i32, } fn main() { let optional_point = Some(Point { x: 100, y: 200 }); // Solution 1: Matching over the `Option` (not `&Option`) but without moving // out of the `Some` variant. match optional_point { Some(ref p) => println!("Coordinates are ...
rust
MIT
7850a73d95c02840f4ab3bf8d9571b08410e5467
2026-01-04T15:31:58.719144Z
false
rust-lang/rustlings
https://github.com/rust-lang/rustlings/blob/7850a73d95c02840f4ab3bf8d9571b08410e5467/solutions/12_options/options1.rs
solutions/12_options/options1.rs
// This function returns how much ice cream there is left in the fridge. // If it's before 22:00 (24-hour system), then 5 scoops are left. At 22:00, // someone eats it all, so no ice cream is left (value 0). Return `None` if // `hour_of_day` is higher than 23. fn maybe_ice_cream(hour_of_day: u16) -> Option<u16> { m...
rust
MIT
7850a73d95c02840f4ab3bf8d9571b08410e5467
2026-01-04T15:31:58.719144Z
false
rust-lang/rustlings
https://github.com/rust-lang/rustlings/blob/7850a73d95c02840f4ab3bf8d9571b08410e5467/solutions/12_options/options2.rs
solutions/12_options/options2.rs
fn main() { // You can optionally experiment here. } #[cfg(test)] mod tests { #[test] fn simple_option() { let target = "rustlings"; let optional_target = Some(target); // if-let if let Some(word) = optional_target { assert_eq!(word, target); } } ...
rust
MIT
7850a73d95c02840f4ab3bf8d9571b08410e5467
2026-01-04T15:31:58.719144Z
false
rust-lang/rustlings
https://github.com/rust-lang/rustlings/blob/7850a73d95c02840f4ab3bf8d9571b08410e5467/solutions/06_move_semantics/move_semantics4.rs
solutions/06_move_semantics/move_semantics4.rs
fn main() { // You can optionally experiment here. } #[cfg(test)] mod tests { #[test] fn move_semantics4() { let mut x = Vec::new(); let y = &mut x; // `y` used here. y.push(42); // The mutable reference `y` is not used anymore, // therefore a new reference c...
rust
MIT
7850a73d95c02840f4ab3bf8d9571b08410e5467
2026-01-04T15:31:58.719144Z
false
rust-lang/rustlings
https://github.com/rust-lang/rustlings/blob/7850a73d95c02840f4ab3bf8d9571b08410e5467/solutions/06_move_semantics/move_semantics3.rs
solutions/06_move_semantics/move_semantics3.rs
fn fill_vec(mut vec: Vec<i32>) -> Vec<i32> { // ^^^ added vec.push(88); vec } fn main() { // You can optionally experiment here. } #[cfg(test)] mod tests { use super::*; #[test] fn move_semantics3() { let vec0 = vec![22, 44, 66]; let vec1 = fill_vec(vec0); as...
rust
MIT
7850a73d95c02840f4ab3bf8d9571b08410e5467
2026-01-04T15:31:58.719144Z
false
rust-lang/rustlings
https://github.com/rust-lang/rustlings/blob/7850a73d95c02840f4ab3bf8d9571b08410e5467/solutions/06_move_semantics/move_semantics5.rs
solutions/06_move_semantics/move_semantics5.rs
#![allow(clippy::ptr_arg)] // Borrows instead of taking ownership. // It is recommended to use `&str` instead of `&String` here. But this is // enough for now because we didn't handle strings yet. fn get_char(data: &String) -> char { data.chars().last().unwrap() } // Takes ownership instead of borrowing. fn strin...
rust
MIT
7850a73d95c02840f4ab3bf8d9571b08410e5467
2026-01-04T15:31:58.719144Z
false
rust-lang/rustlings
https://github.com/rust-lang/rustlings/blob/7850a73d95c02840f4ab3bf8d9571b08410e5467/solutions/06_move_semantics/move_semantics2.rs
solutions/06_move_semantics/move_semantics2.rs
fn fill_vec(vec: Vec<i32>) -> Vec<i32> { let mut vec = vec; vec.push(88); vec } fn main() { // You can optionally experiment here. } #[cfg(test)] mod tests { use super::*; #[test] fn move_semantics2() { let vec0 = vec![22, 44, 66]; // Cloning `vec0` so that the clone is...
rust
MIT
7850a73d95c02840f4ab3bf8d9571b08410e5467
2026-01-04T15:31:58.719144Z
false
rust-lang/rustlings
https://github.com/rust-lang/rustlings/blob/7850a73d95c02840f4ab3bf8d9571b08410e5467/solutions/06_move_semantics/move_semantics1.rs
solutions/06_move_semantics/move_semantics1.rs
fn fill_vec(vec: Vec<i32>) -> Vec<i32> { let mut vec = vec; // ^^^ added vec.push(88); vec } fn main() { // You can optionally experiment here. } #[cfg(test)] mod tests { use super::*; #[test] fn move_semantics1() { let vec0 = vec![22, 44, 66]; let vec1 = fill_vec(v...
rust
MIT
7850a73d95c02840f4ab3bf8d9571b08410e5467
2026-01-04T15:31:58.719144Z
false
rust-lang/rustlings
https://github.com/rust-lang/rustlings/blob/7850a73d95c02840f4ab3bf8d9571b08410e5467/src/info_file.rs
src/info_file.rs
use anyhow::{Context, Error, Result, bail}; use serde::Deserialize; use std::{fs, io::ErrorKind}; use crate::{embedded::EMBEDDED_FILES, exercise::RunnableExercise}; /// Deserialized from the `info.toml` file. #[derive(Deserialize)] pub struct ExerciseInfo { /// Exercise's unique name. pub name: String, //...
rust
MIT
7850a73d95c02840f4ab3bf8d9571b08410e5467
2026-01-04T15:31:58.719144Z
false
rust-lang/rustlings
https://github.com/rust-lang/rustlings/blob/7850a73d95c02840f4ab3bf8d9571b08410e5467/src/dev.rs
src/dev.rs
use anyhow::{Context, Result, bail}; use clap::Subcommand; use std::path::PathBuf; mod check; mod new; mod update; #[derive(Subcommand)] pub enum DevCommands { /// Create a new project for community exercises New { /// The path to create the project in path: PathBuf, /// Don't try to i...
rust
MIT
7850a73d95c02840f4ab3bf8d9571b08410e5467
2026-01-04T15:31:58.719144Z
false
rust-lang/rustlings
https://github.com/rust-lang/rustlings/blob/7850a73d95c02840f4ab3bf8d9571b08410e5467/src/cargo_toml.rs
src/cargo_toml.rs
use anyhow::{Context, Result}; use std::path::Path; use crate::{exercise::RunnableExercise, info_file::ExerciseInfo}; /// Initial capacity of the bins buffer. pub const BINS_BUFFER_CAPACITY: usize = 1 << 14; /// Return the start and end index of the content of the list `bin = […]`. /// bin = [xxxxxxxxxxxxxxxxx] /// ...
rust
MIT
7850a73d95c02840f4ab3bf8d9571b08410e5467
2026-01-04T15:31:58.719144Z
false
rust-lang/rustlings
https://github.com/rust-lang/rustlings/blob/7850a73d95c02840f4ab3bf8d9571b08410e5467/src/list.rs
src/list.rs
use anyhow::{Context, Result}; use crossterm::{ QueueableCommand, cursor, event::{ self, DisableMouseCapture, EnableMouseCapture, Event, KeyCode, KeyEventKind, MouseEventKind, }, terminal::{ DisableLineWrap, EnableLineWrap, EnterAlternateScreen, LeaveAlternateScreen, disable_raw_...
rust
MIT
7850a73d95c02840f4ab3bf8d9571b08410e5467
2026-01-04T15:31:58.719144Z
false
rust-lang/rustlings
https://github.com/rust-lang/rustlings/blob/7850a73d95c02840f4ab3bf8d9571b08410e5467/src/exercise.rs
src/exercise.rs
use anyhow::Result; use crossterm::{ QueueableCommand, style::{Attribute, Color, ResetColor, SetAttribute, SetForegroundColor}, }; use std::io::{self, StdoutLock, Write}; use crate::{ cmd::CmdRunner, term::{self, CountedWrite, file_path, terminal_file_link, write_ansi}, }; /// The initial capacity of ...
rust
MIT
7850a73d95c02840f4ab3bf8d9571b08410e5467
2026-01-04T15:31:58.719144Z
false
rust-lang/rustlings
https://github.com/rust-lang/rustlings/blob/7850a73d95c02840f4ab3bf8d9571b08410e5467/src/app_state.rs
src/app_state.rs
use anyhow::{Context, Error, Result, bail}; use crossterm::{QueueableCommand, cursor, terminal}; use std::{ collections::HashSet, env, fs::{File, OpenOptions}, io::{Read, Seek, StdoutLock, Write}, path::{MAIN_SEPARATOR_STR, Path}, process::{Command, Stdio}, sync::{ atomic::{AtomicUsi...
rust
MIT
7850a73d95c02840f4ab3bf8d9571b08410e5467
2026-01-04T15:31:58.719144Z
false
rust-lang/rustlings
https://github.com/rust-lang/rustlings/blob/7850a73d95c02840f4ab3bf8d9571b08410e5467/src/watch.rs
src/watch.rs
use anyhow::{Error, Result}; use notify::{Config, RecommendedWatcher, RecursiveMode, Watcher}; use std::{ io::{self, Write}, path::Path, sync::{ atomic::{AtomicBool, Ordering::Relaxed}, mpsc::channel, }, time::Duration, }; use crate::{ app_state::{AppState, ExercisesProgress}, ...
rust
MIT
7850a73d95c02840f4ab3bf8d9571b08410e5467
2026-01-04T15:31:58.719144Z
false
rust-lang/rustlings
https://github.com/rust-lang/rustlings/blob/7850a73d95c02840f4ab3bf8d9571b08410e5467/src/run.rs
src/run.rs
use anyhow::Result; use crossterm::{ QueueableCommand, style::{Color, ResetColor, SetForegroundColor}, }; use std::{ io::{self, Write}, process::ExitCode, }; use crate::{ app_state::{AppState, ExercisesProgress}, exercise::{OUTPUT_CAPACITY, RunnableExercise, solution_link_line}, }; pub fn run(...
rust
MIT
7850a73d95c02840f4ab3bf8d9571b08410e5467
2026-01-04T15:31:58.719144Z
false
rust-lang/rustlings
https://github.com/rust-lang/rustlings/blob/7850a73d95c02840f4ab3bf8d9571b08410e5467/src/init.rs
src/init.rs
use anyhow::{Context, Result, bail}; use crossterm::{ QueueableCommand, style::{Attribute, Color, ResetColor, SetAttribute, SetForegroundColor}, }; use serde::Deserialize; use std::{ env::set_current_dir, fs::{self, create_dir}, io::{self, Write}, path::{Path, PathBuf}, process::{Command, St...
rust
MIT
7850a73d95c02840f4ab3bf8d9571b08410e5467
2026-01-04T15:31:58.719144Z
false
rust-lang/rustlings
https://github.com/rust-lang/rustlings/blob/7850a73d95c02840f4ab3bf8d9571b08410e5467/src/embedded.rs
src/embedded.rs
use anyhow::{Context, Error, Result}; use std::{ fs::{self, create_dir}, io, }; use crate::info_file::ExerciseInfo; /// Contains all embedded files. pub static EMBEDDED_FILES: EmbeddedFiles = rustlings_macros::include_files!(); // Files related to one exercise. struct ExerciseFiles { // The content of th...
rust
MIT
7850a73d95c02840f4ab3bf8d9571b08410e5467
2026-01-04T15:31:58.719144Z
false
rust-lang/rustlings
https://github.com/rust-lang/rustlings/blob/7850a73d95c02840f4ab3bf8d9571b08410e5467/src/main.rs
src/main.rs
use anyhow::{Context, Result, bail}; use app_state::StateFileStatus; use clap::{Parser, Subcommand}; use std::{ io::{self, IsTerminal, Write}, path::Path, process::ExitCode, }; use term::{clear_terminal, press_enter_prompt}; use self::{app_state::AppState, dev::DevCommands, info_file::InfoFile}; mod app_s...
rust
MIT
7850a73d95c02840f4ab3bf8d9571b08410e5467
2026-01-04T15:31:58.719144Z
false
rust-lang/rustlings
https://github.com/rust-lang/rustlings/blob/7850a73d95c02840f4ab3bf8d9571b08410e5467/src/term.rs
src/term.rs
use crossterm::{ Command, QueueableCommand, cursor::MoveTo, style::{Attribute, Color, ResetColor, SetAttribute, SetForegroundColor}, terminal::{Clear, ClearType}, }; use std::{ fmt, fs, io::{self, BufRead, StdoutLock, Write}, }; use crate::app_state::CheckProgress; pub struct MaxLenWriter<'a, ...
rust
MIT
7850a73d95c02840f4ab3bf8d9571b08410e5467
2026-01-04T15:31:58.719144Z
false
rust-lang/rustlings
https://github.com/rust-lang/rustlings/blob/7850a73d95c02840f4ab3bf8d9571b08410e5467/src/cmd.rs
src/cmd.rs
use anyhow::{Context, Result, bail}; use serde::Deserialize; use std::{ io::{Read, pipe}, path::PathBuf, process::{Command, Stdio}, }; /// Run a command with a description for a possible error and append the merged stdout and stderr. /// The boolean in the returned `Result` is true if the command's exit st...
rust
MIT
7850a73d95c02840f4ab3bf8d9571b08410e5467
2026-01-04T15:31:58.719144Z
false
rust-lang/rustlings
https://github.com/rust-lang/rustlings/blob/7850a73d95c02840f4ab3bf8d9571b08410e5467/src/dev/update.rs
src/dev/update.rs
use anyhow::{Context, Result}; use std::fs; use crate::{ cargo_toml::updated_cargo_toml, info_file::{ExerciseInfo, InfoFile}, }; // Update the `Cargo.toml` file. fn update_cargo_toml( exercise_infos: &[ExerciseInfo], cargo_toml_path: &str, exercise_path_prefix: &[u8], ) -> Result<()> { let cur...
rust
MIT
7850a73d95c02840f4ab3bf8d9571b08410e5467
2026-01-04T15:31:58.719144Z
false
rust-lang/rustlings
https://github.com/rust-lang/rustlings/blob/7850a73d95c02840f4ab3bf8d9571b08410e5467/src/dev/check.rs
src/dev/check.rs
use anyhow::{Context, Error, Result, anyhow, bail}; use std::{ cmp::Ordering, collections::HashSet, fs::{self, OpenOptions, read_dir}, io::{self, Read, Write}, path::{Path, PathBuf}, process::{Command, Stdio}, thread, }; use crate::{ CURRENT_FORMAT_VERSION, cargo_toml::{BINS_BUFFER_...
rust
MIT
7850a73d95c02840f4ab3bf8d9571b08410e5467
2026-01-04T15:31:58.719144Z
false
rust-lang/rustlings
https://github.com/rust-lang/rustlings/blob/7850a73d95c02840f4ab3bf8d9571b08410e5467/src/dev/new.rs
src/dev/new.rs
use anyhow::{Context, Result, bail}; use std::{ env::set_current_dir, fs::{self, create_dir}, path::Path, process::Command, }; use crate::{CURRENT_FORMAT_VERSION, init::RUST_ANALYZER_TOML}; // Create a directory relative to the current directory and print its path. fn create_rel_dir(dir_name: &str, cu...
rust
MIT
7850a73d95c02840f4ab3bf8d9571b08410e5467
2026-01-04T15:31:58.719144Z
false
rust-lang/rustlings
https://github.com/rust-lang/rustlings/blob/7850a73d95c02840f4ab3bf8d9571b08410e5467/src/list/scroll_state.rs
src/list/scroll_state.rs
pub struct ScrollState { n_rows: usize, max_n_rows_to_display: usize, selected: Option<usize>, offset: usize, scroll_padding: usize, max_scroll_padding: usize, } impl ScrollState { pub fn new(n_rows: usize, selected: Option<usize>, max_scroll_padding: usize) -> Self { Self { ...
rust
MIT
7850a73d95c02840f4ab3bf8d9571b08410e5467
2026-01-04T15:31:58.719144Z
false
rust-lang/rustlings
https://github.com/rust-lang/rustlings/blob/7850a73d95c02840f4ab3bf8d9571b08410e5467/src/list/state.rs
src/list/state.rs
use anyhow::{Context, Result}; use crossterm::{ QueueableCommand, cursor::{MoveTo, MoveToNextLine}, style::{ Attribute, Attributes, Color, ResetColor, SetAttribute, SetAttributes, SetForegroundColor, }, terminal::{self, BeginSynchronizedUpdate, Clear, ClearType, EndSynchronizedUpdate}, }; us...
rust
MIT
7850a73d95c02840f4ab3bf8d9571b08410e5467
2026-01-04T15:31:58.719144Z
false
rust-lang/rustlings
https://github.com/rust-lang/rustlings/blob/7850a73d95c02840f4ab3bf8d9571b08410e5467/src/watch/notify_event.rs
src/watch/notify_event.rs
use anyhow::{Context, Result}; use notify::{ Event, EventKind, event::{AccessKind, AccessMode, MetadataKind, ModifyKind, RenameMode}, }; use std::{ sync::{ atomic::Ordering::Relaxed, mpsc::{RecvTimeoutError, Sender, SyncSender, sync_channel}, }, thread, time::Duration, }; use su...
rust
MIT
7850a73d95c02840f4ab3bf8d9571b08410e5467
2026-01-04T15:31:58.719144Z
false
rust-lang/rustlings
https://github.com/rust-lang/rustlings/blob/7850a73d95c02840f4ab3bf8d9571b08410e5467/src/watch/state.rs
src/watch/state.rs
use anyhow::{Context, Result}; use crossterm::{ QueueableCommand, style::{ Attribute, Attributes, Color, ResetColor, SetAttribute, SetAttributes, SetForegroundColor, }, terminal, }; use std::{ io::{self, Read, StdoutLock, Write}, sync::mpsc::{Sender, SyncSender, sync_channel}, thread...
rust
MIT
7850a73d95c02840f4ab3bf8d9571b08410e5467
2026-01-04T15:31:58.719144Z
false
rust-lang/rustlings
https://github.com/rust-lang/rustlings/blob/7850a73d95c02840f4ab3bf8d9571b08410e5467/src/watch/terminal_event.rs
src/watch/terminal_event.rs
use crossterm::event::{self, Event, KeyCode, KeyEventKind}; use std::sync::{ atomic::Ordering::Relaxed, mpsc::{Receiver, Sender}, }; use super::{EXERCISE_RUNNING, WatchEvent}; pub enum InputEvent { Next, Run, Hint, List, CheckAll, Reset, Quit, } pub fn terminal_event_handler( ...
rust
MIT
7850a73d95c02840f4ab3bf8d9571b08410e5467
2026-01-04T15:31:58.719144Z
false
rust-lang/rustlings
https://github.com/rust-lang/rustlings/blob/7850a73d95c02840f4ab3bf8d9571b08410e5467/tests/integration_tests.rs
tests/integration_tests.rs
use std::{ env::{self, consts::EXE_SUFFIX}, process::{Command, Stdio}, str::from_utf8, }; enum Output<'a> { FullStdout(&'a str), PartialStdout(&'a str), PartialStderr(&'a str), } use Output::*; #[derive(Default)] struct Cmd<'a> { current_dir: Option<&'a str>, args: &'a [&'a str], ...
rust
MIT
7850a73d95c02840f4ab3bf8d9571b08410e5467
2026-01-04T15:31:58.719144Z
false
rust-lang/rustlings
https://github.com/rust-lang/rustlings/blob/7850a73d95c02840f4ab3bf8d9571b08410e5467/tests/test_exercises/exercises/test_success.rs
tests/test_exercises/exercises/test_success.rs
fn main() { println!("Output from `main` function"); } #[cfg(test)] mod tests { #[test] fn passes() {} }
rust
MIT
7850a73d95c02840f4ab3bf8d9571b08410e5467
2026-01-04T15:31:58.719144Z
false
rust-lang/rustlings
https://github.com/rust-lang/rustlings/blob/7850a73d95c02840f4ab3bf8d9571b08410e5467/tests/test_exercises/exercises/test_failure.rs
tests/test_exercises/exercises/test_failure.rs
fn main() {} #[cfg(test)] mod tests { #[test] fn fails() { assert!(false); } }
rust
MIT
7850a73d95c02840f4ab3bf8d9571b08410e5467
2026-01-04T15:31:58.719144Z
false
rust-lang/rustlings
https://github.com/rust-lang/rustlings/blob/7850a73d95c02840f4ab3bf8d9571b08410e5467/tests/test_exercises/exercises/compilation_failure.rs
tests/test_exercises/exercises/compilation_failure.rs
fn main() { let }
rust
MIT
7850a73d95c02840f4ab3bf8d9571b08410e5467
2026-01-04T15:31:58.719144Z
false
rust-lang/rustlings
https://github.com/rust-lang/rustlings/blob/7850a73d95c02840f4ab3bf8d9571b08410e5467/tests/test_exercises/exercises/compilation_success.rs
tests/test_exercises/exercises/compilation_success.rs
fn main() {}
rust
MIT
7850a73d95c02840f4ab3bf8d9571b08410e5467
2026-01-04T15:31:58.719144Z
false
rust-lang/rustlings
https://github.com/rust-lang/rustlings/blob/7850a73d95c02840f4ab3bf8d9571b08410e5467/tests/test_exercises/exercises/not_in_info.rs
tests/test_exercises/exercises/not_in_info.rs
fn main() {}
rust
MIT
7850a73d95c02840f4ab3bf8d9571b08410e5467
2026-01-04T15:31:58.719144Z
false
rust-lang/rustlings
https://github.com/rust-lang/rustlings/blob/7850a73d95c02840f4ab3bf8d9571b08410e5467/exercises/19_smart_pointers/cow1.rs
exercises/19_smart_pointers/cow1.rs
// This exercise explores the `Cow` (Clone-On-Write) smart pointer. It can // enclose and provide immutable access to borrowed data and clone the data // lazily when mutation or ownership is required. The type is designed to work // with general borrowed data via the `Borrow` trait. use std::borrow::Cow; fn abs_all(i...
rust
MIT
7850a73d95c02840f4ab3bf8d9571b08410e5467
2026-01-04T15:31:58.719144Z
false
rust-lang/rustlings
https://github.com/rust-lang/rustlings/blob/7850a73d95c02840f4ab3bf8d9571b08410e5467/exercises/19_smart_pointers/box1.rs
exercises/19_smart_pointers/box1.rs
// At compile time, Rust needs to know how much space a type takes up. This // becomes problematic for recursive types, where a value can have as part of // itself another value of the same type. To get around the issue, we can use a // `Box` - a smart pointer used to store data on the heap, which also allows us // to ...
rust
MIT
7850a73d95c02840f4ab3bf8d9571b08410e5467
2026-01-04T15:31:58.719144Z
false
rust-lang/rustlings
https://github.com/rust-lang/rustlings/blob/7850a73d95c02840f4ab3bf8d9571b08410e5467/exercises/19_smart_pointers/arc1.rs
exercises/19_smart_pointers/arc1.rs
// In this exercise, we are given a `Vec` of `u32` called `numbers` with values // ranging from 0 to 99. We would like to use this set of numbers within 8 // different threads simultaneously. Each thread is going to get the sum of // every eighth value with an offset. // // The first thread (offset 0), will sum 0, 8, 1...
rust
MIT
7850a73d95c02840f4ab3bf8d9571b08410e5467
2026-01-04T15:31:58.719144Z
false
rust-lang/rustlings
https://github.com/rust-lang/rustlings/blob/7850a73d95c02840f4ab3bf8d9571b08410e5467/exercises/19_smart_pointers/rc1.rs
exercises/19_smart_pointers/rc1.rs
// In this exercise, we want to express the concept of multiple owners via the // `Rc<T>` type. This is a model of our solar system - there is a `Sun` type and // multiple `Planet`s. The planets take ownership of the sun, indicating that // they revolve around the sun. use std::rc::Rc; #[derive(Debug)] struct Sun; #...
rust
MIT
7850a73d95c02840f4ab3bf8d9571b08410e5467
2026-01-04T15:31:58.719144Z
false
rust-lang/rustlings
https://github.com/rust-lang/rustlings/blob/7850a73d95c02840f4ab3bf8d9571b08410e5467/exercises/22_clippy/clippy1.rs
exercises/22_clippy/clippy1.rs
// The Clippy tool is a collection of lints to analyze your code so you can // catch common mistakes and improve your Rust code. // // For these exercises, the code will fail to compile when there are Clippy // warnings. Check Clippy's suggestions from the output to solve the exercise. fn main() { // TODO: Fix the...
rust
MIT
7850a73d95c02840f4ab3bf8d9571b08410e5467
2026-01-04T15:31:58.719144Z
false
rust-lang/rustlings
https://github.com/rust-lang/rustlings/blob/7850a73d95c02840f4ab3bf8d9571b08410e5467/exercises/22_clippy/clippy3.rs
exercises/22_clippy/clippy3.rs
// Here are some more easy Clippy fixes so you can see its utility. // TODO: Fix all the Clippy lints. #[allow(unused_variables, unused_assignments)] fn main() { let my_option: Option<&str> = None; // Assume that you don't know the value of `my_option`. // In the case of `Some`, we want to print its value....
rust
MIT
7850a73d95c02840f4ab3bf8d9571b08410e5467
2026-01-04T15:31:58.719144Z
false
rust-lang/rustlings
https://github.com/rust-lang/rustlings/blob/7850a73d95c02840f4ab3bf8d9571b08410e5467/exercises/22_clippy/clippy2.rs
exercises/22_clippy/clippy2.rs
fn main() { let mut res = 42; let option = Some(12); // TODO: Fix the Clippy lint. for x in option { res += x; } println!("{res}"); }
rust
MIT
7850a73d95c02840f4ab3bf8d9571b08410e5467
2026-01-04T15:31:58.719144Z
false
rust-lang/rustlings
https://github.com/rust-lang/rustlings/blob/7850a73d95c02840f4ab3bf8d9571b08410e5467/exercises/09_strings/strings1.rs
exercises/09_strings/strings1.rs
// TODO: Fix the compiler error without changing the function signature. fn current_favorite_color() -> String { "blue" } fn main() { let answer = current_favorite_color(); println!("My current favorite color is {answer}"); }
rust
MIT
7850a73d95c02840f4ab3bf8d9571b08410e5467
2026-01-04T15:31:58.719144Z
false
rust-lang/rustlings
https://github.com/rust-lang/rustlings/blob/7850a73d95c02840f4ab3bf8d9571b08410e5467/exercises/09_strings/strings2.rs
exercises/09_strings/strings2.rs
// TODO: Fix the compiler error in the `main` function without changing this function. fn is_a_color_word(attempt: &str) -> bool { attempt == "green" || attempt == "blue" || attempt == "red" } fn main() { let word = String::from("green"); // Don't change this line. if is_a_color_word(word) { print...
rust
MIT
7850a73d95c02840f4ab3bf8d9571b08410e5467
2026-01-04T15:31:58.719144Z
false
rust-lang/rustlings
https://github.com/rust-lang/rustlings/blob/7850a73d95c02840f4ab3bf8d9571b08410e5467/exercises/09_strings/strings3.rs
exercises/09_strings/strings3.rs
fn trim_me(input: &str) -> &str { // TODO: Remove whitespace from both ends of a string. } fn compose_me(input: &str) -> String { // TODO: Add " world!" to the string! There are multiple ways to do this. } fn replace_me(input: &str) -> String { // TODO: Replace "cars" in the string with "balloons". } fn ...
rust
MIT
7850a73d95c02840f4ab3bf8d9571b08410e5467
2026-01-04T15:31:58.719144Z
false
rust-lang/rustlings
https://github.com/rust-lang/rustlings/blob/7850a73d95c02840f4ab3bf8d9571b08410e5467/exercises/09_strings/strings4.rs
exercises/09_strings/strings4.rs
// Calls of this function should be replaced with calls of `string_slice` or `string`. fn placeholder() {} fn string_slice(arg: &str) { println!("{arg}"); } fn string(arg: String) { println!("{arg}"); } // TODO: Here are a bunch of values - some are `String`, some are `&str`. // Your task is to replace `plac...
rust
MIT
7850a73d95c02840f4ab3bf8d9571b08410e5467
2026-01-04T15:31:58.719144Z
false
rust-lang/rustlings
https://github.com/rust-lang/rustlings/blob/7850a73d95c02840f4ab3bf8d9571b08410e5467/exercises/20_threads/threads3.rs
exercises/20_threads/threads3.rs
use std::{sync::mpsc, thread, time::Duration}; struct Queue { first_half: Vec<u32>, second_half: Vec<u32>, } impl Queue { fn new() -> Self { Self { first_half: vec![1, 2, 3, 4, 5], second_half: vec![6, 7, 8, 9, 10], } } } fn send_tx(q: Queue, tx: mpsc::Sender<u...
rust
MIT
7850a73d95c02840f4ab3bf8d9571b08410e5467
2026-01-04T15:31:58.719144Z
false
rust-lang/rustlings
https://github.com/rust-lang/rustlings/blob/7850a73d95c02840f4ab3bf8d9571b08410e5467/exercises/20_threads/threads2.rs
exercises/20_threads/threads2.rs
// Building on the last exercise, we want all of the threads to complete their // work. But this time, the spawned threads need to be in charge of updating a // shared value: `JobStatus.jobs_done` use std::{sync::Arc, thread, time::Duration}; struct JobStatus { jobs_done: u32, } fn main() { // TODO: `Arc` is...
rust
MIT
7850a73d95c02840f4ab3bf8d9571b08410e5467
2026-01-04T15:31:58.719144Z
false
rust-lang/rustlings
https://github.com/rust-lang/rustlings/blob/7850a73d95c02840f4ab3bf8d9571b08410e5467/exercises/20_threads/threads1.rs
exercises/20_threads/threads1.rs
// This program spawns multiple threads that each runs for at least 250ms, and // each thread returns how much time it took to complete. The program should // wait until all the spawned threads have finished and should collect their // return values into a vector. use std::{ thread, time::{Duration, Instant}, ...
rust
MIT
7850a73d95c02840f4ab3bf8d9571b08410e5467
2026-01-04T15:31:58.719144Z
false
rust-lang/rustlings
https://github.com/rust-lang/rustlings/blob/7850a73d95c02840f4ab3bf8d9571b08410e5467/exercises/13_error_handling/errors2.rs
exercises/13_error_handling/errors2.rs
// Say we're writing a game where you can buy items with tokens. All items cost // 5 tokens, and whenever you purchase items there is a processing fee of 1 // token. A player of the game will type in how many items they want to buy, and // the `total_cost` function will calculate the total cost of the items. Since // t...
rust
MIT
7850a73d95c02840f4ab3bf8d9571b08410e5467
2026-01-04T15:31:58.719144Z
false
rust-lang/rustlings
https://github.com/rust-lang/rustlings/blob/7850a73d95c02840f4ab3bf8d9571b08410e5467/exercises/13_error_handling/errors6.rs
exercises/13_error_handling/errors6.rs
// Using catch-all error types like `Box<dyn Error>` isn't recommended for // library code where callers might want to make decisions based on the error // content instead of printing it out or propagating it further. Here, we define // a custom error type to make it possible for callers to decide what to do next // wh...
rust
MIT
7850a73d95c02840f4ab3bf8d9571b08410e5467
2026-01-04T15:31:58.719144Z
false
rust-lang/rustlings
https://github.com/rust-lang/rustlings/blob/7850a73d95c02840f4ab3bf8d9571b08410e5467/exercises/13_error_handling/errors3.rs
exercises/13_error_handling/errors3.rs
// This is a program that is trying to use a completed version of the // `total_cost` function from the previous exercise. It's not working though! // Why not? What should we do to fix it? use std::num::ParseIntError; // Don't change this function. fn total_cost(item_quantity: &str) -> Result<i32, ParseIntError> { ...
rust
MIT
7850a73d95c02840f4ab3bf8d9571b08410e5467
2026-01-04T15:31:58.719144Z
false
rust-lang/rustlings
https://github.com/rust-lang/rustlings/blob/7850a73d95c02840f4ab3bf8d9571b08410e5467/exercises/13_error_handling/errors1.rs
exercises/13_error_handling/errors1.rs
// TODO: This function refuses to generate text to be printed on a nametag if // you pass it an empty string. It'd be nicer if it explained what the problem // was instead of just returning `None`. Thankfully, Rust has a similar // construct to `Option` that can be used to express error conditions. Change // the functi...
rust
MIT
7850a73d95c02840f4ab3bf8d9571b08410e5467
2026-01-04T15:31:58.719144Z
false
rust-lang/rustlings
https://github.com/rust-lang/rustlings/blob/7850a73d95c02840f4ab3bf8d9571b08410e5467/exercises/13_error_handling/errors4.rs
exercises/13_error_handling/errors4.rs
#[derive(PartialEq, Debug)] enum CreationError { Negative, Zero, } #[derive(PartialEq, Debug)] struct PositiveNonzeroInteger(u64); impl PositiveNonzeroInteger { fn new(value: i64) -> Result<Self, CreationError> { // TODO: This function shouldn't always return an `Ok`. // Read the tests bel...
rust
MIT
7850a73d95c02840f4ab3bf8d9571b08410e5467
2026-01-04T15:31:58.719144Z
false
rust-lang/rustlings
https://github.com/rust-lang/rustlings/blob/7850a73d95c02840f4ab3bf8d9571b08410e5467/exercises/13_error_handling/errors5.rs
exercises/13_error_handling/errors5.rs
// This exercise is an altered version of the `errors4` exercise. It uses some // concepts that we won't get to until later in the course, like `Box` and the // `From` trait. It's not important to understand them in detail right now, but // you can read ahead if you like. For now, think of the `Box<dyn ???>` type as //...
rust
MIT
7850a73d95c02840f4ab3bf8d9571b08410e5467
2026-01-04T15:31:58.719144Z
false
rust-lang/rustlings
https://github.com/rust-lang/rustlings/blob/7850a73d95c02840f4ab3bf8d9571b08410e5467/exercises/23_conversions/from_str.rs
exercises/23_conversions/from_str.rs
// This is similar to the previous `from_into` exercise. But this time, we'll // implement `FromStr` and return errors instead of falling back to a default // value. Additionally, upon implementing `FromStr`, you can use the `parse` // method on strings to generate an object of the implementor type. You can read // mor...
rust
MIT
7850a73d95c02840f4ab3bf8d9571b08410e5467
2026-01-04T15:31:58.719144Z
false
rust-lang/rustlings
https://github.com/rust-lang/rustlings/blob/7850a73d95c02840f4ab3bf8d9571b08410e5467/exercises/23_conversions/try_from_into.rs
exercises/23_conversions/try_from_into.rs
// `TryFrom` is a simple and safe type conversion that may fail in a controlled // way under some circumstances. Basically, this is the same as `From`. The main // difference is that this should return a `Result` type instead of the target // type itself. You can read more about it in the documentation: // https://doc....
rust
MIT
7850a73d95c02840f4ab3bf8d9571b08410e5467
2026-01-04T15:31:58.719144Z
false
rust-lang/rustlings
https://github.com/rust-lang/rustlings/blob/7850a73d95c02840f4ab3bf8d9571b08410e5467/exercises/23_conversions/using_as.rs
exercises/23_conversions/using_as.rs
// Type casting in Rust is done via the usage of the `as` operator. // Note that the `as` operator is not only used when type casting. It also helps // with renaming imports. fn average(values: &[f64]) -> f64 { let total = values.iter().sum::<f64>(); // TODO: Make a conversion before dividing. total / valu...
rust
MIT
7850a73d95c02840f4ab3bf8d9571b08410e5467
2026-01-04T15:31:58.719144Z
false
rust-lang/rustlings
https://github.com/rust-lang/rustlings/blob/7850a73d95c02840f4ab3bf8d9571b08410e5467/exercises/23_conversions/from_into.rs
exercises/23_conversions/from_into.rs
// The `From` trait is used for value-to-value conversions. If `From` is // implemented, an implementation of `Into` is automatically provided. // You can read more about it in the documentation: // https://doc.rust-lang.org/std/convert/trait.From.html #[derive(Debug)] struct Person { name: String, age: u8, } ...
rust
MIT
7850a73d95c02840f4ab3bf8d9571b08410e5467
2026-01-04T15:31:58.719144Z
false
rust-lang/rustlings
https://github.com/rust-lang/rustlings/blob/7850a73d95c02840f4ab3bf8d9571b08410e5467/exercises/23_conversions/as_ref_mut.rs
exercises/23_conversions/as_ref_mut.rs
// AsRef and AsMut allow for cheap reference-to-reference conversions. Read more // about them at https://doc.rust-lang.org/std/convert/trait.AsRef.html and // https://doc.rust-lang.org/std/convert/trait.AsMut.html, respectively. // Obtain the number of bytes (not characters) in the given argument // (`.len()` returns...
rust
MIT
7850a73d95c02840f4ab3bf8d9571b08410e5467
2026-01-04T15:31:58.719144Z
false
rust-lang/rustlings
https://github.com/rust-lang/rustlings/blob/7850a73d95c02840f4ab3bf8d9571b08410e5467/exercises/07_structs/structs1.rs
exercises/07_structs/structs1.rs
struct ColorRegularStruct { // TODO: Add the fields that the test `regular_structs` expects. // What types should the fields have? What are the minimum and maximum values for RGB colors? } struct ColorTupleStruct(/* TODO: Add the fields that the test `tuple_structs` expects */); #[derive(Debug)] struct UnitSt...
rust
MIT
7850a73d95c02840f4ab3bf8d9571b08410e5467
2026-01-04T15:31:58.719144Z
false
rust-lang/rustlings
https://github.com/rust-lang/rustlings/blob/7850a73d95c02840f4ab3bf8d9571b08410e5467/exercises/07_structs/structs2.rs
exercises/07_structs/structs2.rs
#[derive(Debug)] struct Order { name: String, year: u32, made_by_phone: bool, made_by_mobile: bool, made_by_email: bool, item_number: u32, count: u32, } fn create_order_template() -> Order { Order { name: String::from("Bob"), year: 2019, made_by_phone: false, ...
rust
MIT
7850a73d95c02840f4ab3bf8d9571b08410e5467
2026-01-04T15:31:58.719144Z
false
rust-lang/rustlings
https://github.com/rust-lang/rustlings/blob/7850a73d95c02840f4ab3bf8d9571b08410e5467/exercises/07_structs/structs3.rs
exercises/07_structs/structs3.rs
// Structs contain data, but can also have logic. In this exercise, we have // defined the `Package` struct, and we want to test some logic attached to it. #[derive(Debug)] struct Package { sender_country: String, recipient_country: String, weight_in_grams: u32, } impl Package { fn new(sender_country:...
rust
MIT
7850a73d95c02840f4ab3bf8d9571b08410e5467
2026-01-04T15:31:58.719144Z
false
rust-lang/rustlings
https://github.com/rust-lang/rustlings/blob/7850a73d95c02840f4ab3bf8d9571b08410e5467/exercises/17_tests/tests1.rs
exercises/17_tests/tests1.rs
// Tests are important to ensure that your code does what you think it should // do. fn is_even(n: i64) -> bool { n % 2 == 0 } fn main() { // You can optionally experiment here. } #[cfg(test)] mod tests { // TODO: Import `is_even`. You can use a wildcard to import everything in // the outer module. ...
rust
MIT
7850a73d95c02840f4ab3bf8d9571b08410e5467
2026-01-04T15:31:58.719144Z
false