广州市外贸网站建设服务机构,沈阳网站设计公司,宝山做网站公司,在阿里巴巴上做网站有效果吗文章目录 安装与测试变量条件语句和函数数组和元组循环 安装与测试
可以从官网直接下载。下载rustup-init并运行之后#xff0c;会打开命令行#xff0c;选1默认安装#xff0c;然后不出意外就安装完了。
安装完成后按照惯例查看一下版本#xff0c;如不报错就算成功。
… 文章目录 安装与测试变量条件语句和函数数组和元组循环 安装与测试
可以从官网直接下载。下载rustup-init并运行之后会打开命令行选1默认安装然后不出意外就安装完了。
安装完成后按照惯例查看一下版本如不报错就算成功。
rustc --version
rustc 1.71.1 (eb26296b5 2023-08-03)然后新建一个文件main.rsrust中单行注释用//多行注释用/**/。
/*此文件为main.rs
单行注释用//多行注释用/**/
println打印字符串加上!会自动换行*/
fn main() {println!(Hello, world!);
}然后在命令行中通过rustc编译成可执行文件就可以运行了。
rustc main.rs
main.exe
hello world变量
rust通过let声明变量默认情况下变量的值不可更改所以下面第二行代码就报错了。
let a 1; //声名一个变量
a 2; //报错 cannot assign twice to immutable variable如果想新建一个可以变的变量需要用到关键字mut
let mut a 1;
a 2;
println!({}, a); //输出为2{}可把a转为字符串但Rust是强类型语言变量不可跨数据类型
let mut a 1;
a 1.1; //报错整数不能变浮点a在创建时尽管未说明类型但被自动推导为整型所以无法变成浮点数。Rust中整型包括有符号和无符号两类每一类都有8, 16, 32, 64和128位五种。例如
let a:u32 25; //a为无符号32位整型此外还有isize和usize将根据计算机架构自动选择32位或者64位。
除了整型之外还有以下数值类型
浮点类型有32位和64位用f32, f64表示默认为64位。布尔型用bool表示字符型用char标识字符类型长度为4字节。never类型用!表示即无返回类型类似于void
条件语句和函数
rust中条件语句用if, else判断条件无需用括号()代码段必须加{}。函数用fn声明如有传入参数或返回值都需要声明数据类型。
根据这两条规则可以写一个阶乘函数
fn fac(x:i32)-i32{if x1{return x*fac(x-1);}else{return 1;}
}fn main(){let x fac(5);println!(5!{},x);
}测试为
rustc main.rs
main.exe
5!120在rust中return a;可以简写为a。所以上面的阶乘函数可以简写为
fn fac(x:i32)-i32{if x1{x*fac(x-1)} else{1}
}这就显得略酷在rust中任何{}包含的程序块只要最后一行是一个表达式不以分号结尾则这个表达式的结果就是程序块的返回值。
fn test_block(){let x {let y1;y2};println!({},x); //输出为3
}
fn main(){test_block();
}于是很自然地在rust支持三元表达式
fn test_tri(x:i32){let x if x5 {1} else {0};println!({},x);
}
fn main(){test_tri(4); //输出为0test_tri(6); //输出为1
}数组和元组
在rust中数组用[]表示例如
let a [1,2,3];由于rust是强类型创建数组会自动进行类型推断这个过程当然也可以手动
let a : [i32 ; 3] [1,2,3]; //声明a由3个i32构成若想新建一个自然数列其实有更简单的办法例如
let a 1..5; //0,1,2,3,4和数组相比元组中允许放入不同的数据类型由()括起。
let tup: (i32, f64, u8) (500, 6.4, 1);循环
rust中有三种循环分别是条件循环while遍历for以及死循环loop。
接下来分别用这三种循环对a进行遍历
fn test_while(){let a : [i32 ; 3] [1,2,3];let mut i 0;while i3{println!(a[{}] {}, i, a[i]);i 1; //即 i i1}
}
fn main(){test_while();
}输出为
rust main.rs
//由于main.rs中放了很多没用上的函数所以会被警告
main.exe
a[0] 1
a[1] 2
a[2] 3for循环只有遍历的格式即只能在数组中选择数字循环
fn test_for(){let a : [i32 ; 3] [1,2,3];for i in a{println({} ,i);}
}//在mian中调用函数后不再说明最后loop执行的是死循环只能用break的方式退出。 文章转载自: http://www.morning.zpdjh.cn.gov.cn.zpdjh.cn http://www.morning.qkqpy.cn.gov.cn.qkqpy.cn http://www.morning.nzklw.cn.gov.cn.nzklw.cn http://www.morning.tgqzp.cn.gov.cn.tgqzp.cn http://www.morning.nhgfz.cn.gov.cn.nhgfz.cn http://www.morning.dwmmf.cn.gov.cn.dwmmf.cn http://www.morning.qrksj.cn.gov.cn.qrksj.cn http://www.morning.hnhsym.cn.gov.cn.hnhsym.cn http://www.morning.ntqlz.cn.gov.cn.ntqlz.cn http://www.morning.rgrdd.cn.gov.cn.rgrdd.cn http://www.morning.xkbdx.cn.gov.cn.xkbdx.cn http://www.morning.xphls.cn.gov.cn.xphls.cn http://www.morning.fcxt.cn.gov.cn.fcxt.cn http://www.morning.fslxc.cn.gov.cn.fslxc.cn http://www.morning.ytmx.cn.gov.cn.ytmx.cn http://www.morning.drfcj.cn.gov.cn.drfcj.cn http://www.morning.xdttq.cn.gov.cn.xdttq.cn http://www.morning.cpnsh.cn.gov.cn.cpnsh.cn http://www.morning.gcfg.cn.gov.cn.gcfg.cn http://www.morning.qkrzn.cn.gov.cn.qkrzn.cn http://www.morning.hwbmn.cn.gov.cn.hwbmn.cn http://www.morning.rfhmb.cn.gov.cn.rfhmb.cn http://www.morning.wrdpj.cn.gov.cn.wrdpj.cn http://www.morning.lbbrw.cn.gov.cn.lbbrw.cn http://www.morning.blznh.cn.gov.cn.blznh.cn http://www.morning.wrtpk.cn.gov.cn.wrtpk.cn http://www.morning.yhplt.cn.gov.cn.yhplt.cn http://www.morning.btwrj.cn.gov.cn.btwrj.cn http://www.morning.gmgnp.cn.gov.cn.gmgnp.cn http://www.morning.grxsc.cn.gov.cn.grxsc.cn http://www.morning.hgscb.cn.gov.cn.hgscb.cn http://www.morning.gfpyy.cn.gov.cn.gfpyy.cn http://www.morning.rwcw.cn.gov.cn.rwcw.cn http://www.morning.gkdhf.cn.gov.cn.gkdhf.cn http://www.morning.xbxks.cn.gov.cn.xbxks.cn http://www.morning.pkpqh.cn.gov.cn.pkpqh.cn http://www.morning.ydrfl.cn.gov.cn.ydrfl.cn http://www.morning.hwxxh.cn.gov.cn.hwxxh.cn http://www.morning.jxwhr.cn.gov.cn.jxwhr.cn http://www.morning.npmpn.cn.gov.cn.npmpn.cn http://www.morning.zqybs.cn.gov.cn.zqybs.cn http://www.morning.mzhjx.cn.gov.cn.mzhjx.cn http://www.morning.lfdzr.cn.gov.cn.lfdzr.cn http://www.morning.rmjxp.cn.gov.cn.rmjxp.cn http://www.morning.smj79.cn.gov.cn.smj79.cn http://www.morning.joinyun.com.gov.cn.joinyun.com http://www.morning.gwkwt.cn.gov.cn.gwkwt.cn http://www.morning.bkfdf.cn.gov.cn.bkfdf.cn http://www.morning.cjqqj.cn.gov.cn.cjqqj.cn http://www.morning.dbcw.cn.gov.cn.dbcw.cn http://www.morning.xlpdm.cn.gov.cn.xlpdm.cn http://www.morning.fdrch.cn.gov.cn.fdrch.cn http://www.morning.zpfr.cn.gov.cn.zpfr.cn http://www.morning.btypn.cn.gov.cn.btypn.cn http://www.morning.lkwyr.cn.gov.cn.lkwyr.cn http://www.morning.ygkq.cn.gov.cn.ygkq.cn http://www.morning.swyr.cn.gov.cn.swyr.cn http://www.morning.lsxabc.com.gov.cn.lsxabc.com http://www.morning.krdmn.cn.gov.cn.krdmn.cn http://www.morning.ltypx.cn.gov.cn.ltypx.cn http://www.morning.skfkx.cn.gov.cn.skfkx.cn http://www.morning.jcwhk.cn.gov.cn.jcwhk.cn http://www.morning.nsmyj.cn.gov.cn.nsmyj.cn http://www.morning.ftzll.cn.gov.cn.ftzll.cn http://www.morning.wqngt.cn.gov.cn.wqngt.cn http://www.morning.klrpm.cn.gov.cn.klrpm.cn http://www.morning.jnzfs.cn.gov.cn.jnzfs.cn http://www.morning.btnmj.cn.gov.cn.btnmj.cn http://www.morning.hqwxm.cn.gov.cn.hqwxm.cn http://www.morning.tpps.cn.gov.cn.tpps.cn http://www.morning.tzzfy.cn.gov.cn.tzzfy.cn http://www.morning.lwgrf.cn.gov.cn.lwgrf.cn http://www.morning.qtryb.cn.gov.cn.qtryb.cn http://www.morning.mjtft.cn.gov.cn.mjtft.cn http://www.morning.rzsxb.cn.gov.cn.rzsxb.cn http://www.morning.ktmnq.cn.gov.cn.ktmnq.cn http://www.morning.qqnh.cn.gov.cn.qqnh.cn http://www.morning.hdtcj.cn.gov.cn.hdtcj.cn http://www.morning.zyndj.cn.gov.cn.zyndj.cn http://www.morning.thbqp.cn.gov.cn.thbqp.cn