avatarJohn Philip

Summary

The undefined website presents the 86th issue of the Rustlings series, focusing on solutions and explanations for Rustlings exercises, specifically addressing the clippy3.rs challenge in Rust with Clippy lints.

Abstract

This issue of the Rustlings series, titled "Rustlings: clippy3.rs #Issue86 — Clippy in Rust," provides a detailed walkthrough for solving the clippy3.rs exercise. It follows the previous challenge, clippy2.rs, and offers a step-by-step solution to common Rust code issues detected by the Clippy tool. The article includes code examples with before and after improvements, demonstrating the utility of Clippy in enhancing Rust code quality. Readers are also directed to the Rust Playground for hands-on experimentation and are encouraged to engage with the Rust community by sharing their knowledge and following the publication.

Opinions

  • The Clippy tool is highly regarded for its ability to catch common mistakes and improve Rust code.
  • The author values the importance of practical code examples to illustrate the impact of Clippy's suggestions.
  • There is an emphasis on community engagement, with invitations to write for the Rustaceans publication and share the article with others.
  • The use of if let syntax is preferred over is_none and unwrap for handling Option types in Rust.
  • The article suggests that using clear on a vector is a more idiomatic way to empty it compared to resizing with a default value.
  • The author acknowledges the convenience of the Rust Playground for readers to test and understand the code changes.
  • The article concludes with a call to action for readers to follow the publication and anticipate future challenges.

Rustlings: clippy3.rs #Issue86 — Clippy in Rust

Rustlings Challenge: clippy3.rs Solution Walkthrough

Image by Brave.com

This is the Eighty-sixth (86th) issue of the Rustlings series. In this issue, we provide solutions to Rustlings exercises along with detailed explanations. In this issue we will solve the challenge on clippy3.rs.

Previous challenge #Issue 85

The Clippy tool is a collection of lints to analyze your code so you can catch common mistakes and improve your Rust code.

Challenge:

// clippy3.rs
// 
// Here's a couple more easy Clippy fixes, so you can see its utility.
// No hints.

// I AM NOT DONE

#[allow(unused_variables, unused_assignments)]
fn main() {
    let my_option: Option<()> = None;
    if my_option.is_none() {
        my_option.unwrap();
    }

    let my_arr = &[
        -1, -2, -3
        -4, -5, -6
    ];
    println!("My array! Here it is: {:?}", my_arr);

    let my_empty_vec = vec![1, 2, 3, 4, 5].resize(0, 5);
    println!("This Vec is empty, see? {:?}", my_empty_vec);

    let mut value_a = 45;
    let mut value_b = 66;
    // Let's swap these two!
    value_a = value_b;
    value_b = value_a;
    println!("value a: {}; value b: {}", value_a, value_b);
}

Solution:

#[allow(unused_variables, unused_assignments)]
fn main() {
    let my_option: Option<()> = None;
    if let Some(x) = my_option {}

    let my_arr = &[
        -1, -2, -3
        -4, -5, -6
    ];
    println!("My array! Here it is: {:?}", my_arr);

    let my_empty_vec = vec![1, 2, 3, 4, 5].clear();
    println!("This Vec is empty, see? {:?}", my_empty_vec);

    let mut value_a = 45;
    let mut value_b = 66;
    // Let's swap these two!
    // Alternatively you can use std::mem::swap(&mut value_a, &mut value_b);
    let swap_a = value_a;
    value_a = value_b;
    value_b = swap_a;
    println!("value a: {}; value b: {}", value_a, value_b);
    // My array! Here it is: [-1, -2, -7, -5, -6]
    // This Vec is empty, see? ()
    // value a: 66; value b: 45
}

You can experiment with the code on Rust Playground.

Resources

Before you go

Thank you for taking the time to read through this challenge. We invite you to share your knowledge of Rust as well. If you found this article valuable, please don’t hesitate to share it with others. Don’t forget to follow the publication and give the article some claps 👏.

Thank you, and we look forward to seeing you for the next challenges!

More reads

Rust
Rustlang
Rust Programming Language
Programming
Rustling
Recommended from ReadMedium