How to get the parameters of main function

Time:2021-1-26

The main function of trust is different from the main function of other languages. It has no input parameter and return value.

Take Hello world as an example.

c#

using System;

namespace ConsoleApp
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Hello World!");
        }
    }
}

c++

#include <iostream>

int main()
{
    std::cout << "Hello World!\n";
}

rust

fn main() {
    println!("Hello, world!");
}

Main functions in other languages have input parameters or return values. Then how to get the input parameter of trust and how to deal with the return value.

Trust has special functions to handle input parameters and return values.

fn main() {
    for arg in std::env::args()
    {
        println!(arg);
    }

    std::process::exit(0);
}

The entry parameter of process exit function is the return value. Function args can get all the input parameters.

This work adoptsCC agreementReprint must indicate the author and the link of this article

Recommended Today

020_CSS3

catalog How to learn CSS What is CSS History of development quick get start Advantages of CSS Three ways to import CSS Expansion: two ways of writing external style selector Basic selector Hierarchy selector Structure pseudo class selector attribute selectors Beautify web page elements Why beautify web pages Span label: for the text that needs […]