Feature Name: optional_function_arguments
Start Date: 2019-02-05
RFC PR:
Rust Issue:
Introduction
This RFC adds the possibility of using optional argument.
Motivation
In most languages today there is an optional argument system.
Indeed, in rust it already exists, but it is not really used by his complexity.
This feature can be really good for rust by simplifying rust programming and existing crates.
Detailed design
// This system use the already existing Option statement
// Because the usage of Option statement all already libraries works without any problem without any updates.
fn example_with_optional(i: i32,c: Option<i32>) {
// Body of the function
}
// But here we can simply do that instead of writing Some of None
fn main() {
example_with_optional(12);
example_with_optional(12,13);
// This can be compiled to:
// example_with_optional(12,None);
// example_with_optional(12,Some(13));
}
Alternatives
The alternatives is to use a more complete system like in Java, C++ or other languages, but this alternative require a Rebuild of rust functions system:
// This is more radical
fn example_with_optional(r: i32) {
// Body of the function
}
// Here the addition of a new argument
fn example_with_optional(r: i32,e: i32) {
// Body of the function
}
// And if we use that we can use different types
fn example_with_optional(r: i32,e: f32) {
// Body of the function
}
fn main() {
example_with_optional(1,1.4);
example_with_optional(1);
example_with_optional(1,78);
// This code will work without the duplicated name error
}
// A method of implementation:
// This code can be compiled like that:
// This is more radical
fn example_with_optional__1(r: i32) {
// Body of the function
}
// Here the addition of a new argument
fn example_with_optional__2(r: i32,e: i32) {
// Body of the function
}
// And if we use that we can use different types
fn example_with_optional__3(r: i32,e: f32) {
// Body of the function
}
fn main() {
example_with_optional__3(1,1.4);
example_with_optional__1(1);
example_with_optional__2(1,78);
}
Conclusion
This can be a great feature for all rust programmers.
Indeed, this feature makes rust more ergonomic, simple and gives to beginners marks from other languages
I don't know if this RFC can be accepted.
Thank's for reading.
ccgauche.