use nim language

first nim in vscode

echo "hello world"
# f6 to run with nim extension
echo("ok bye",10,'\n',[1,1,1])
# 算术支持和c一致,区别于python
var a:int = 1
echo(a)
# var a = "str" static type not dynamic type support
a = 22222
echo(a)
let immutable_variable = 3.14
# immutable_variable = 22222
echo(immutable_variable)

if true :
    echo("true")
elif false:
    echo("false")
else:
    echo("never reachable")
var i:int = 10

while i > 0 :
    echo(i)
    i=i-1

examples

var
    a = 10
    b = 3.33
    ch = '\n'
    c = "c style str"
    d = true
    e:float
    str = """
    my name is
    etcix.
    """
    arr:array[0..8,int] = [1,1,2,3,4,5,5,6,7]
echo(arr.type)
echo(ch.type,c.type,str.type,d.type)
str.add(c)
c.add(str)
echo(a.toFloat + b + e) # type convert
# import modules like python code style
import strutils
proc fuck*(str:string):void=
    echo("fuck function fuck ",str)

let data = readLine(stdin)
fuck(data)

let num1 = stdin.readLine
let num2 = stdin.readLine
echo(num1.parseInt+num2.parseInt)
# case pattern match 
import strutils
echo("input a number: ")
let num = stdin.readLine.parseInt

case num:
    # include [1,100]
    of 1..100:
        echo("百人斩中")
    of 101..1000:
        echo("千人斩中")
    of 1001..10000:
        debugEcho("万人斩")# debug Echo
    else:
        echo("not support number")
discard """
    this code will discard as block comment
"""
const
    debug = true
when debug:
    echo("debug info enable")


# tuple and seq
var
    child:tuple[name:string,age:int]
    today:tuple[sum:string,temp:float]
child = (name:"etcix",age:21)
today = (sum:"to do nim tutorial",temp:34.0)
today.sum = "finished sum of today"
echo(today.sum)
var 
    drinks:seq[string]
drinks = @["water","juice","what ever"]
drinks.add("coco")
if "coco" in drinks:
    echo("yes I want it ",drinks[3])
# define my data type
type 
    Name = string #alias of string using Name
    Age = int
    Person = tuple[name:Name,age:Age]
    StrIntTup = tuple
        first:string
        second:int
var
    me:Person=(name:"etcix",age:21)
me.age = 999

# with c function call using FFI:foreign function interface
proc strcmp(a, b: cstring): cint {.importc: "strcmp", nodecl.}
let cmp = strcmp("C?", "Easy!")
echo(cmp)

reference

learn x in y mins: https://learnxinyminutes.com/docs/nim/

你可能感兴趣的:(zig学习,开发语言)