Zum Inhalt springen

Rust Challanges from the Rust Bytes Newsletter

https://weeklyrust.substack.com/i/142061717/spot-the-bug

struct Counter {
count: i32,
}
impl Counter {
fn increment(&self) {
self.count += 1;
}
}

https://weeklyrust.substack.com/i/141571387/spot-the-bug

trait MyTrait {
fn print(&self);
}
struct MyStruct;
impl MyTrait for MyStruct {
fn print(&self) {
println!("Hello from MyStruct");
}
}
fn main() {
let my_trait: MyTrait = MyStruct;
my_trait.print();
}

https://weeklyrust.substack.com/i/141922904/spot-the-bug

fn main() {
let one = "rust";
let other = "go";
println!("{}", greater_of_two(one, other));
}
fn greater_of_two(one: &str, other: &str) -> &str {
if one.len() > other.len() {
return one;
}
return other;
}

https://weeklyrust.substack.com/i/142751344/spot-the-bug

fn main() {
let v = vec![1, 2, 3];
let sum = v.iter().map(|x| x * 2).sum();
println!("Sum: {sum}");
}

https://weeklyrust.substack.com/i/142907963/spot-the-bug

use std::collections::HashMap;
struct UInt64Counter<Key> {
count_by_key: HashMap<Key, u64>
}
impl<Key> UInt64Counter<Key> {
fn new() -> Self {
return UInt64Counter {
count_by_key: HashMap::new()
}
}
fn add(&mut self, key: Key, delta: u64) {
*self.count_by_key.entry(key).or_default() += delta;
}
}

https://weeklyrust.substack.com/i/143120825/spot-the-bug

fn main() {
let marks = vec![10, 9, 8, 4, 6];
let sum = 0;
for mark in marks {
sum = sum + mark;
}
println!("Summ of all marks {marks:?} is {sum}");
}

https://weeklyrust.substack.com/i/143417297/spot-the-bug

trait MathOperation {
fn operate(&self, x: i32, y: i32) -> i32;
}
struct Addition;
impl MathOperation for Addition {
fn operate(&self, x: i32, y: i32) {
x + y
}
}
fn main() {
let add_op = Addition;
let result = perform_operation(&add_op, 10, 5);
println!("Addition result {result}");
}
fn perform_operation(op: &MathOperation, x: i32, y: i32) -> i32 {
op.operate(x, y)
}

https://weeklyrust.substack.com/i/143580795/spot-the-bug

fn main() {
let mut numbers = vec![1, 2, 3, 4, 5];
for num in numbers {
numbers.push(num * 2);
}
println!("Doubled numbers: {:?}", numbers);
}

https://weeklyrust.substack.com/i/143887608/spot-the-bug

fn factorial(n: u32) -> u32 {
if n == 0 {
1
} else {
factorial(n)
}
}
fn main() {
let result = factorial(5);
println!("Factorial of 5: {result}")
}

https://weeklyrust.substack.com/i/144074478/spot-the-bug

trait Animal {
fn make_sound(&self);
}
struct Dog {
name: String;
}
impl Animal for Dog {
fn make_sound(&self) {
println!("{} says: Woof!", self.name);
}
}
struct Cat {
name: String;
}
impl Animal for Cat {
fn make_sound(&self) {
println!("{} says: Meow!", self.name);
}
}
fn main() {
let dog = Dog {
name: String::from("Rex"),
};
let cat = Cat {
name: String::from("Lisa"),
};
let animals: Vec<dyn Animal> = vec![dog, cat];
for animal in animals {
animal.make_sound();
}
}

https://weeklyrust.substack.com/i/144196403/challenge-rust-quiz

fn main() {
let mut numbers = vec![1, 2, 3];
numbers.resize(5, 2);
println!("{numbers:?}");
}
  1. [1, 2, 3, 5, 5]
  2. [1, 2, 3, 2, 2]
  3. [1, 2, 3, 0, 0]

https://weeklyrust.substack.com/i/143756901/challenge-rust-quiz

fn main() {
let mut rust_bytes = String::from("Rust Bytes");
let rust_bytes_ref = &mut rust_bytes;
rust_bytes.push_str(" is great.");
println!("{}", rust_bytes_ref);
}
  1. Rust Bytes is great.
  2. Compiler Error

https://weeklyrust.substack.com/i/144468047/challenge-rust-quiz

fn main() {
let my_vector = vec![Some("Rust"), None, Some("Bytes"), None]
for element in my_vector.iter() {
let value = element.unwrap_or({
println!("Value not found");
"None"
})
}
}

How often is “Value not found” printed?

  1. 2 (times)
  2. 4 (times)
  3. 1

https://weeklyrust.substack.com/i/144542233/challenge-rust-quiz

fn main() {
let (.., x, y) = (0, 1, ..);
print!("{}", b"066"[y][x]);
}
  1. The program exhibits undefined behavior
  2. The program does not compile
  3. The program is guaranteed to output: []

https://weeklyrust.substack.com/i/144650924/challenge-rust-quiz

fn main() {
let mut y = 6;
--y;
pritnln!("{}{}", --y, --y);
}
  1. The program does not compile
  2. The program outputs: 44
  3. The program outputs: 66

https://weeklyrust.substack.com/i/144748323/rust-tip-flatten-your-options-with-confidence

fn main() {
let maybe_nested_message: Option<Option<&str>> = Some(Some("Rust Bytes is awesome"));
let message_option = maybe_nested_message.flatten();
println!("{:?}", message_option.unwrap_or("Unknown"));
}

https://weeklyrust.substack.com/i/145006568/challenge-rust-quiz

trait Printable {
fn print_value(&self);
}
impl Printable for (u32, u32) {
fn print_value(&self) {
print!("1");
}
}
impl Printable for (i32, i32,) {
fn print_value(&self) {
print!("2");
}
}
fn main() {
(0, 0,).print_value();
(0, 0).print_value();
}

https://weeklyrust.substack.com/i/145332199/challenge-rust-quiz

trait Or {
fn f(self):
}
struct T;
impl or for &T {
fn f(self) {
print!("1");
}
}
impl Or for &&&&T {
fn f(self) {
print!("2");
}
}
fn main() {
let t = T;
let wt = &T;
let wwt = &&T;
let wwwt = &&&T;
let wwwwt = &&&&T;
let wwwwwt = &&&&&T;
t.f()
wt.f()
wwt.f()
wwwt.f()
wwwwt.f()
wwwwwt.f()
}

https://weeklyrust.substack.com/i/145597050/challenge-rust-quiz

Write an algorithm that will identify valid IPv4 addresses in dot-decimal format. IPs should be considered valid if they consist of four octets, with values between 0 and 255, inclusive.

Valid IPs:

1.2.3.4
123.45.67.89

Invalid IPs:

1.2.3
1.2.3.4.5
123.456.78.90
123.045.067.089
01.02.03.05

https://weeklyrust.substack.com/i/149879242/rust-challenge Calculate the Hamming Distance between two DNA strands.


https://weeklyrust.substack.com/i/161813352/rust-challenge *JSON Log analyzer