Help for the creation of an RFC

Preface

I have notified some improvements for rust.
But I don't know how to make an RFC.

Sorry for my English I'm french :frowning:

Optionals arguments, Overloading and Multitypes

Overloading exemple:


     fn substring(string: String,i: usize) -> String {
         return (&string[i..string.len()]).to_string();
     }
 
     fn substring(string: String,x: usize,y: usize) -> String {
         return (&string[x..y]).to_string();
     }

     fn substring(string: &str,i: usize) -> String {
         return (&string[i..string.len()]).to_string();
     }
 
     fn substring(string: &str,x: usize,y: usize) -> String {
         return (&string[x..y]).to_string();
     }

With the overloading you can make two functions with differents arguments length or different arguments types.
The return type can be overloaded as well.

Optionals arguments example:


    //test is optional if it is not passed it value equals to undefined or something like that
    fn testprinter(string: String,?test: usize) {
        //I don't know the condition for null testing in rust so i going to make a javascript like code
        if test==undefined {
            printlnt!("{}",string);
        } else {
            println!("{} with {}",string,test);
        }
    }

    //Test is optional and if it is not passed it's value is 32
    fn testprinter(string: String,test = 32: usize) {
        println!("{} with {}",string,test);
    }

Optional arguments can easily replace the Option<> in rust function arguments.

Multitype example

    //Test can be typed has all the types between {}
    fn testprinter(string: String,test: {usize,i32,f64,String,&str}) {
        println!("{} with {}",string,test);
        //We can test the with the expression I have invented
        if (test instanceof usize) {
            println!("{}","Type is usize");
        }
    }

    //Test can be typed has all the types in rust
    fn testprinter(string: String,test: *) {
        println!("{} with {}",string,test);
        //We can test the with the expression I have invented
        if (test instanceof usize) {
            println!("{}","Type is usize");
        }
    }

Multitypes can be useful for many things and can improve rust ergonomy.

Finally

I don't know how to make an RFC and if my ideas has been already added.

I think all this tweaks can make rust better.

Best regards ccgauche.

You probably want to have a look at the RFC repo to see the process you usually follow when proposing a change to the language. Typically you'll make a thread on the internals forum, giving you a chance to explore the idea and hear people's feedback.

Function overloading and default or keyword arguments are a pretty popular request because it's used quite successfully in other languages (C++, Python, C#, etc). I think the reason it hasn't been implemented in the past is because overloading, generics, and type inference don't work well together.

Some more links which you may find useful:

EDIT: Thanks for asking this question, by the way. I came across lots of really interesting content while trying to find links to help point you to prior attempts at function overloading in Rust.

1 Like

The former is an enum. Right now you need to write out all the cases nominally, though there are discussions about potentially adding anonymous ones in the future. Your if (test instanceof usize) { is currently spelled something like if let MyEnum::USize(_) = test {.

For the latter you want Any. A direct translation of your example:

fn testprinter(string: String, test: impl Display + Any + 'static) {
    println!("{} with {}", string, test);
    let test: &dyn Any = &test;
    if test.is::<usize>() {
        println!("{}", "Type is usize");
    }
}

You can see it working at Rust Playground

1 Like