In the following code, why do I have to write Self::function_name instead of just function name? I mean, seriously, this is impl block and if in this block there is a function with that name why the compiler cannot deduce which function I have in mind? (collect_primary is the name of the function).
pub struct Parser
{
primary: String,
}
impl Parser
{
pub fn parse(s:&str)
{
for a_char in s.chars()
{
match a_char
{
'0'..='9' => collect_primary(&a_char),**//Why this will not find collect_primary???**
'+' | '-' => println!("We got a sign: {}", a_char),
_ => println!("Not a number!")
}
}
}
fn collect_primary(ch:&char)
{
}
}
OK, I understand, but then, if I don't keep it in the impl block it somewhat looses the "belongs to" property and becomes a "free" function, which I wouldn't like to do.
I simply don't understand why the compiler complains about it.
I tend to prefer free functions, but didn't always. Coming to Rust after doing a lot of OO development, at first I too wanted my functions to 'belong' to a struct.
Realizing two things helped me change:
Rust's level of encapsulation is not the struct or impl block, but the module. Free functions have total access to the struct's private fields and functions, and they do not have to be public at all. They can even be located in a sub-module and still have total access. This means encapsulation is a complete non-reason for choosing associated functions over free functions. Instead, reorganizing your modules to map closer to what you would consider an OO 'class' in other languages leads to better organized code in my opinion.
If a function doesn't operate on a specific instance of a struct, then making it an associated function doesn't buy you much even in terms of syntactic sugar, and can actually hurt chances for reuse. This can prevent you from seeing the function might be useful outside the context you originally envisioned for it.
I tend to limit associated functions to only methods that take some form of self parameter, and constructors.
Then the impl block seems bit pointless tbh.
Also, thanks for making me aware that free functions do have an access to struct's private fields. I was totally unaware of that.
Functionally yes, but you would lose a lot of the benefits that method syntax gives you like method chaining and using the "object" mindset instead of a more procedural one.
Yes, I'm aware of that. I'm simply questioning point of having impl block where in fact everything can be done without it, or, its usage is of very, very limited point. (After what I've learned about free functions)