-
Notifications
You must be signed in to change notification settings - Fork 0
Mysterious Error
The way I tested out some of this code I have written, I attempted to make a new bank
inside the main()
function like so:
#[derive(Debug)]
struct Account {
id: u32,
balance: i32,
holder: String,
}
impl Account {
fn new(id: u32, holder: String) -> Self {
Account {
id,
holder,
balance: 0,
}
}
}
#[derive(Debug)]
struct Bank {
accounts: Vec<Account>,
}
impl Bank {
fn new() -> Self {
Bank { accounts: vec![] }
}
}
fn main() {
let bank = Bank::new();
println!("{:#?}", bank);
}
Then I do a cargo run -q
and there is a couple of warnings which is totally fine but bank
is printed out:
warning: associated function `new` is never used
--> src/main.rs:9:8
|
8 | impl Account {
| ------------ associated function in this implementation
9 | fn new(id: u32, holder: String) -> Self {
| ^^^
warning: field `accounts` is never read
--> src/main.rs:20:5
|
19 | struct Bank {
| ---- field in this struct
20 | accounts: Vec<Account>,
| ^^^^^^^^
|
= note: `Bank` has a derived impl for the trait `Debug`, but this is intentionally ignored during dead code analysis
Bank {
accounts: [],
}
And now I will test out Account
:
#[derive(Debug)]
struct Account {
id: u32,
balance: i32,
holder: String,
}
impl Account {
fn new(id: u32, holder: String) -> Self {
Account {
id,
holder,
balance: 0,
}
}
}
#[derive(Debug)]
struct Bank {
accounts: Vec<Account>,
}
impl Bank {
fn new() -> Self {
Bank { accounts: vec![] }
}
}
fn main() {
let bank = Bank::new();
let account = Account::new(1, String::from("me"));
println!("{:#?}", bank);
println!("{:#?}", account);
}
Test it with cargo run -q
and:
warning: field `accounts` is never read
--> src/main.rs:20:5
|
19 | struct Bank {
| ---- field in this struct
20 | accounts: Vec<Account>,
| ^^^^^^^^
|
= note: `Bank` has a derived impl for the trait `Debug`, but this is intentionally ignored during dead code analysis
Bank {
accounts: [],
}
Account {
id: 1,
balance: 0,
holder: "me",
}
Here is where things get interesting. Having to type that whole println will get tiresome after awhile. So I would like to make a helper function right above main()
like so:
#[derive(Debug)]
struct Account {
id: u32,
balance: i32,
holder: String,
}
impl Account {
fn new(id: u32, holder: String) -> Self {
Account {
id,
holder,
balance: 0,
}
}
}
#[derive(Debug)]
struct Bank {
accounts: Vec<Account>,
}
impl Bank {
fn new() -> Self {
Bank { accounts: vec![] }
}
}
fn print_account(account: Account) {
println!("{:#?}", account);
}
fn main() {
let bank = Bank::new();
let account = Account::new(1, String::from("me"));
print_account(account);
}
And...
= note: `Account` has a derived impl for the trait `Debug`, but this is intentionally ignored during dead code analysis
= note: `#[warn(dead_code)]` on by default
warning: field `accounts` is never read
--> src/main.rs:20:5
|
19 | struct Bank {
| ---- field in this struct
20 | accounts: Vec<Account>,
| ^^^^^^^^
|
= note: `Bank` has a derived impl for the trait `Debug`, but this is intentionally ignored during dead code analysis
Account {
id: 1,
balance: 0,
holder: "me",
Now here is the bug we are going to run into.
So all I will do is copy and paste the print_account(account)
The first thing I want to point out about this error is that it appears like nothing is wrong but its clear that Rust is unhappy about something. Please see Wiki on Unexpected Value Updates