Rust:- Build Warning – “warning: field is never read”

Background

Let us quickly troubleshoot a rust build warning.

 

Error Message

Textual


>cargo build
warning: field `person_name` is never read
  --> src\main.rs:27:7
   |
25 | struct Congregant
   |        ---------- field in this struct
26 | {
27 |       person_name: String
   |       ^^^^^^^^^^^
   |
   = note: `Congregant` has a derived impl for the trait `Debug`, but this is intentionally ignored during dead code analysis
   = note: `#[warn(dead_code)]` on by default

Image

 

Source Code

Code Snippet

Here is a snippet of our source code


#[derive(Debug)]
struct Congregant 
{
      person_name: String
      
    , bible_translation: String
    
    , is_kjv: bool
    
    , err:String
    
}

Error Message Review

Textual

= note: `Congregant` has a derived impl for the trait `Debug`, but this is intentionally ignored during dead code analysis
= note: `#[warn(dead_code)]` on by default

Explanation

Conpiler has this to say:-

  1. = note: `Congregant` has a derived impl for the trait `Debug`, but this is intentionally ignored during dead code analysis
    • Yes, you have enabled the debug flag
    • But, I will intentionally ignore the Dubug trait while checking for “dead code”
  2. = note: `#[warn(dead_code)]` on by default
    • #[warn(dead_code)]
      • Warning for dead ( unneeded ) code is on by default
      • In this case, we are setting the person’s name, yet never reading it

 

Remediation

Outline

  1. Source Code
    • Added directives
      • #[allow(dead_code)]
      • #[allow(unused)]
    • Added implementation code for structure display

Source Code

Remediation

Code


impl std::fmt::Display for Congregant
{

	fn fmt
	(
		&self, f: &mut std::fmt::Formatter
	) -> std::fmt::Result 
	{
		
		let buffer:String;
		
		buffer = format!
			(
				  "{0}\r\n\tBible Translation:- {1}\r\n\tIs KJV:- {2}\r\n\tErr:- {3}\r\n"
				, self.person_name
				, self.bible_translation
				, self.is_kjv
				, self.err
			);
			
		let result =
					writeln!
					(
						  f
						, "{}"  
						, buffer
					);
					
		return result;
		
	}

	
}

Online Code IDE

OnlineGDB

  1. RustKJVOnly
    Link

Source Code Control

GitLab

  1. Rust – Exception Handling
    • Link
      Link
    • Files
      • 01) RustKJVOnly.rs

 

Listening

Listenting to who I need to listen to:-

Masta Ace
Soda & Soap (Feat. Jean Grae)
Link

 

Dedication

This is for my __ EM from up the way.

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s