I am working through the Rust book, specifically the "Departments" exercise in section 8.3.
I've become a little confused by what is being returned from a method I've created.
#[derive(Debug, PartialEq, Eq, Hash)]
enum Department {
Sales,
Engineering,
}
#[derive(Debug)]
struct Company {
users: HashMap<Department, Vec<String>>,
}
impl Company {
fn add_user_to_department(&mut self, department: Department, employee: String) {
let users = self.users.entry(department).or_insert(Vec::new());
users.push(employee);
}
fn get_users_in_department(&self, department: Department) -> &[String] {
match self.users.get(&department) {
Some(u) => &u,
None => &[]
}
}
}
At first I thought the method get_users_in_department is returning a slice of the Vec (or in the case of None a static literal).
But in my match statement I am not using the slice syntax, I am returning a borrowed reference to the Vec.
match self.users.get(&department) {
Some(u) => &u,
None => &[]
}
If I was returning a slice wouldn't I have to return Some(u) => &u[..],
I guess my confusion boils down to what the return type of get_users_in_department is. Is it returning a reference to an array of String, or a slice of an array of String. Are these two types interchangeable due to some sort of coercion? If it's possible to differentiate between the two, how and when would I want to return one or the other?