Why is a type annotation needed on a function that declares more generic types than it uses?












0















fn lifetime_tester<A, B, C>(a: Box<A>, b: Box<B>, c: &i32) -> &i32 {
c
}

fn main() {
let a = Box::new(String::from("Test1"));
let b = Box::new(55 as i32);
let c: i32;
{
c = 34 as i32;
}
println!("{}", lifetime_tester(a, b, &c));
}


Error:



error[E0282]: type annotations needed
--> src/main.rs:12:20
|
12 | println!("{}", lifetime_tester(a, b, &c));
| ^^^^^^^^^^^^^^^ cannot infer type for `C`


I am baffled about where a "type annotation" needs to go. I've specified a return type as &i32, I've specified an argument type of &i32 and I've also specified that c is an i32.










share|improve this question




















  • 8





    I'm no rust expert, but it seems like the problem is that you never mention the generic parameter (uppercase) C in the function, so it can't infer its type from the arguments. So you probably need to drop C or explicitly supply the types when calling lifetime_tester

    – happydave
    Nov 13 '18 at 3:55






  • 4





    There is no link between C and c, your Minimal, Complete, and Verifiable example don't make much sense.

    – Stargateur
    Nov 13 '18 at 5:31
















0















fn lifetime_tester<A, B, C>(a: Box<A>, b: Box<B>, c: &i32) -> &i32 {
c
}

fn main() {
let a = Box::new(String::from("Test1"));
let b = Box::new(55 as i32);
let c: i32;
{
c = 34 as i32;
}
println!("{}", lifetime_tester(a, b, &c));
}


Error:



error[E0282]: type annotations needed
--> src/main.rs:12:20
|
12 | println!("{}", lifetime_tester(a, b, &c));
| ^^^^^^^^^^^^^^^ cannot infer type for `C`


I am baffled about where a "type annotation" needs to go. I've specified a return type as &i32, I've specified an argument type of &i32 and I've also specified that c is an i32.










share|improve this question




















  • 8





    I'm no rust expert, but it seems like the problem is that you never mention the generic parameter (uppercase) C in the function, so it can't infer its type from the arguments. So you probably need to drop C or explicitly supply the types when calling lifetime_tester

    – happydave
    Nov 13 '18 at 3:55






  • 4





    There is no link between C and c, your Minimal, Complete, and Verifiable example don't make much sense.

    – Stargateur
    Nov 13 '18 at 5:31














0












0








0








fn lifetime_tester<A, B, C>(a: Box<A>, b: Box<B>, c: &i32) -> &i32 {
c
}

fn main() {
let a = Box::new(String::from("Test1"));
let b = Box::new(55 as i32);
let c: i32;
{
c = 34 as i32;
}
println!("{}", lifetime_tester(a, b, &c));
}


Error:



error[E0282]: type annotations needed
--> src/main.rs:12:20
|
12 | println!("{}", lifetime_tester(a, b, &c));
| ^^^^^^^^^^^^^^^ cannot infer type for `C`


I am baffled about where a "type annotation" needs to go. I've specified a return type as &i32, I've specified an argument type of &i32 and I've also specified that c is an i32.










share|improve this question
















fn lifetime_tester<A, B, C>(a: Box<A>, b: Box<B>, c: &i32) -> &i32 {
c
}

fn main() {
let a = Box::new(String::from("Test1"));
let b = Box::new(55 as i32);
let c: i32;
{
c = 34 as i32;
}
println!("{}", lifetime_tester(a, b, &c));
}


Error:



error[E0282]: type annotations needed
--> src/main.rs:12:20
|
12 | println!("{}", lifetime_tester(a, b, &c));
| ^^^^^^^^^^^^^^^ cannot infer type for `C`


I am baffled about where a "type annotation" needs to go. I've specified a return type as &i32, I've specified an argument type of &i32 and I've also specified that c is an i32.







rust






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 15 '18 at 15:20









Shepmaster

151k13293432




151k13293432










asked Nov 13 '18 at 3:51









the_endianthe_endian

545621




545621








  • 8





    I'm no rust expert, but it seems like the problem is that you never mention the generic parameter (uppercase) C in the function, so it can't infer its type from the arguments. So you probably need to drop C or explicitly supply the types when calling lifetime_tester

    – happydave
    Nov 13 '18 at 3:55






  • 4





    There is no link between C and c, your Minimal, Complete, and Verifiable example don't make much sense.

    – Stargateur
    Nov 13 '18 at 5:31














  • 8





    I'm no rust expert, but it seems like the problem is that you never mention the generic parameter (uppercase) C in the function, so it can't infer its type from the arguments. So you probably need to drop C or explicitly supply the types when calling lifetime_tester

    – happydave
    Nov 13 '18 at 3:55






  • 4





    There is no link between C and c, your Minimal, Complete, and Verifiable example don't make much sense.

    – Stargateur
    Nov 13 '18 at 5:31








8




8





I'm no rust expert, but it seems like the problem is that you never mention the generic parameter (uppercase) C in the function, so it can't infer its type from the arguments. So you probably need to drop C or explicitly supply the types when calling lifetime_tester

– happydave
Nov 13 '18 at 3:55





I'm no rust expert, but it seems like the problem is that you never mention the generic parameter (uppercase) C in the function, so it can't infer its type from the arguments. So you probably need to drop C or explicitly supply the types when calling lifetime_tester

– happydave
Nov 13 '18 at 3:55




4




4





There is no link between C and c, your Minimal, Complete, and Verifiable example don't make much sense.

– Stargateur
Nov 13 '18 at 5:31





There is no link between C and c, your Minimal, Complete, and Verifiable example don't make much sense.

– Stargateur
Nov 13 '18 at 5:31












1 Answer
1






active

oldest

votes


















6














Since there is no link between the arguments to your function and the generic type C, the compiler has no idea which type you mean to replace C in the function call. In this case your code doesn't even use the type C so it doesn't matter, but Rust is big about "local reasoning" - i.e. it won't peek inside your function to determine you're not using C at the syntax checking stage.



You can explicitly tell it what your types are using the "turbofish" operator, ::<>, like this



println!("{}", lifetime_tester::<_, _, i32>(a, b, &c));





share|improve this answer

























    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53273517%2fwhy-is-a-type-annotation-needed-on-a-function-that-declares-more-generic-types-t%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    6














    Since there is no link between the arguments to your function and the generic type C, the compiler has no idea which type you mean to replace C in the function call. In this case your code doesn't even use the type C so it doesn't matter, but Rust is big about "local reasoning" - i.e. it won't peek inside your function to determine you're not using C at the syntax checking stage.



    You can explicitly tell it what your types are using the "turbofish" operator, ::<>, like this



    println!("{}", lifetime_tester::<_, _, i32>(a, b, &c));





    share|improve this answer






























      6














      Since there is no link between the arguments to your function and the generic type C, the compiler has no idea which type you mean to replace C in the function call. In this case your code doesn't even use the type C so it doesn't matter, but Rust is big about "local reasoning" - i.e. it won't peek inside your function to determine you're not using C at the syntax checking stage.



      You can explicitly tell it what your types are using the "turbofish" operator, ::<>, like this



      println!("{}", lifetime_tester::<_, _, i32>(a, b, &c));





      share|improve this answer




























        6












        6








        6







        Since there is no link between the arguments to your function and the generic type C, the compiler has no idea which type you mean to replace C in the function call. In this case your code doesn't even use the type C so it doesn't matter, but Rust is big about "local reasoning" - i.e. it won't peek inside your function to determine you're not using C at the syntax checking stage.



        You can explicitly tell it what your types are using the "turbofish" operator, ::<>, like this



        println!("{}", lifetime_tester::<_, _, i32>(a, b, &c));





        share|improve this answer















        Since there is no link between the arguments to your function and the generic type C, the compiler has no idea which type you mean to replace C in the function call. In this case your code doesn't even use the type C so it doesn't matter, but Rust is big about "local reasoning" - i.e. it won't peek inside your function to determine you're not using C at the syntax checking stage.



        You can explicitly tell it what your types are using the "turbofish" operator, ::<>, like this



        println!("{}", lifetime_tester::<_, _, i32>(a, b, &c));






        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Nov 15 '18 at 15:21









        Shepmaster

        151k13293432




        151k13293432










        answered Nov 13 '18 at 8:27









        Michael AndersonMichael Anderson

        45k693148




        45k693148






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53273517%2fwhy-is-a-type-annotation-needed-on-a-function-that-declares-more-generic-types-t%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Full-time equivalent

            さくらももこ

            13 indicted, 8 arrested in Calif. drug cartel investigation