Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

The type placeholder `_` is not allowed within types on item signatures

Beginner question; and a search couldn't find anything similar.

Background: I'm just practising functions in Rust by making a shuffling function. Program takes in any arguments and shuffles them and stores them in 'result'

Question: I guess I can't use V<_> in a function header so what would I use in this situation?

MCVE:

use std::io;
use std::cmp::Ordering;
use std::env;

fn main()
{
    let mut result = shuffle(env::args().collect());
}//End of main

fn shuffle(args: Vec<_>) -> Vec<_>
{ 
    let mut temp = Vec::with_capacity((args.capacity()));
    while args.len() > 1 
    {
        //LET N REPRESENT A RANDOM NUMBER GENERATED ON EACH ITERATION
        let mut n = 2;
        temp.push(args.swap_remove(n));
    }
    return temp;
}//End of shuffle function

Playground link

like image 586
Singh Avatar asked Sep 28 '22 04:09

Singh


1 Answers

You would convert your function to a generic function:

fn shuffle<T>(args: Vec<T>) -> Vec<T> {

See it in the playpen: http://is.gd/MCCxal

like image 116
oli_obk Avatar answered Sep 29 '22 19:09

oli_obk