0

I am learning rust so some things might seem obvious and easy but I can't understand some things.

I need to have income and expenses variables to change their value, usually I would use a static variable and assign value in an unsafe block.

Here is the code:

fn expense_sum(expense_list: &Vec<u64>, expenses: &mut u64) {
    expenses = &mut (expense_list.iter().sum());
}

fn prompt_expense(expense_list: &mut Vec<u64>, expense_name: &mut Vec<String>, expenses: &mut u64) {
    let expense_input: u64 = 1;
    expense_list.push(expense_input);

    let expense_name1: String = "test1".to_string();

    expense_name.push(expense_name1);

    expense_sum(&expense_list, expenses);
    println!("Total user expenses: {}", expenses);
}

fn main() {
    let mut expense_list: Vec<u64> = Vec::new();
    let mut expense_name: Vec<String> = Vec::new();
    let mut expenses: u64;

    loop {
        prompt_expense(&mut expense_list, &mut expense_name, &mut expenses);
        // USe income and expenses here for analysis
    }
}

I've tested in many ways, but I could not get to pass succesfully the variables to expense_sum and income_sum

5
  • 3
    "usually I would use a static variable and assign value in an unsafe block." You shouldn't usually use unsafe Commented Jul 17, 2019 at 9:36
  • Should have added that I'm doing rust for 2 days, so it's not been a long time using it :D Commented Jul 17, 2019 at 9:41
  • That's fine. Just listen to the compiler, he will tell you what's wrong. E.g. using of uninitialized variables, redundant parentheses, not assigned variables etc. Commented Jul 17, 2019 at 9:42
  • And don't use a static variable. The simplest solution is to pass around what you need in a variable. Commented Jul 17, 2019 at 9:50
  • Make sure to use rustfmt next time. In fact you should use it all the time, when you write code, because it will format your code properly and you won't loose track on your code. Commented Jul 17, 2019 at 10:36

1 Answer 1

2

This is mostly correct; they only real issue preventing it from building is here:

fn expense_sum(expense_list: &Vec<u64>, expenses: &mut u64) {
    expenses = &mut (expense_list.iter().sum());
}

This syntax tries to assign the reference, instead of modifying the actual value referred. You need to dereference and then simply assign:

*expenses = expense_list.iter().sum();

That being said, this is bad practice. It's much better to simply return the value using... the return value of the function:

fn expense_sum(expense_list: &Vec<u64>) -> u64 {
    expense_list.iter().sum()
}

That way the code is shorter and much readable, and you avoid all the unnecessary reference handling. You'll need to modify your prompt_expense function in a similar manner.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you very much! I will follow your suggestions and return the value from the function instead of messing with references.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.