2.1 Rust概念
标识符
The first character is a letter.
The remaining characters are alphanumeric or _.
或
The first character is _.
The identifier is more than one character. _ alone is not an identifier.
The remaining characters are alphanumeric or _.
如果想使用Rust的关键字作为标识符,则需要以r#为前缀
Raw identifiers
Sometimes, you may need to use a name that’s a keyword for another purpose. Maybe you need to call a function named match that is coming from a C library, where ‘match’ is not a keyword. To do this, you can use a “raw identifier.” Raw identifiers start with r#:
let r#fn = "this variable is named 'fn' even though that's a keyword"; // call a function named 'match'
r#match();
2.1 variables and mutability
cargo new variables
[root@itoracle test]# vim variables/src/main.rs
fn main() {
let x = ;
println!("The value of x is: {}", x);
x = ;
println!("The value of x is: {}", x);
}
以上代码将在编译时报错,因为rust默认变量是不能对其值修改的。以下代码是正确的
fn main() {
let mut x = ;
println!("The value of x is: {}", x);
x = ;
println!("The value of x is: {}", x);
}
[root@itoracle test]# cd variables/
[root@itoracle variables]# cargo run
Compiling variables v0.1.0 (/usr/local/automng/src/rust/test/variables)
Finished dev [unoptimized + debuginfo] target(s) in .15s
Running `target/debug/variables`
The value of x is:
The value of x is:
变量与常量
区别:常量必须在编辑时就确定其值,变量可以是表达式,可以在运行时确定其值。
First, you aren’t allowed to use mut with constants. Constants aren’t just immutable by default—they’re always immutable.
You declare constants using the const keyword instead of the let keyword, and the type of the value must be annotated. Just know that you must always annotate the type.
Constants can be declared in any scope, including the global scope, which makes them useful for values that many parts of code need to know about.
The last difference is that constants may be set only to a constant expression, not the result of a function call or any other value that could only be computed at runtime.
Here’s an example of a constant declaration where the constant’s name is MAX_POINTS and its value is set to 100,000. (Rust’s naming convention for constants is to use all uppercase with underscores between words, and underscores can be inserted in numeric literals to improve readability):
[root@itoracle variables]# vim src/main.rs
fn main() {
let mut x = ;
println!("The value of x is: {}", x);
x = ;
println!("The value of x is: {}", x);
const MAX_POINTS: u32 = 100_000;
println!("常量:{}",MAX_POINTS)
}
[root@itoracle variables]# cargo run
Compiling variables v0.1.0 (/usr/local/automng/src/rust/test/variables)
Finished dev [unoptimized + debuginfo] target(s) in .10s
Running `target/debug/variables`
The value of x is:
The value of x is:
常量:
2.4 注释
All programmers strive to make their code easy to understand, but sometimes extra explanation is warranted. In these cases, programmers leave notes, or comments, in their source code that the compiler will ignore but people reading the source code may find useful.
单行注释//
fn main() {
let lucky_number = ; // I’m feeling lucky today
}
fn main() {
// I’m feeling lucky today
let lucky_number = ;
}
2.5 流程控制
if 表达式
fn main() {
let number = ;
if number % == {
println!("number is divisible by 4");
} else if number % == {
println!("number is divisible by 3");
} else if number % == {
println!("number is divisible by 2");
} else {
println!("number is not divisible by 4, 3, or 2");
}
}
if 中使用 let 语句
要求 if 条件各个分支返回的数据类型必须相同
fn main() {
let condition = true;
let number = if condition {
} else {
};
println!("The value of number is: {}", number);
}
loop循环
无返回值
fn main() {
let mut count = ;
loop{
count += ;
if count == {
break;
}
}
}
有返回值
fn main() {
let mut counter = ;
let result = loop {
counter += ;
if counter == {
break counter * ;
}
};
//assert_eq!方法可以在result的值不为20时抛出异常
assert_eq!(result, );
}
比如上面的方法,如果写成 assert_eq!(result, 19);
则在运行时会给出以下错误信息
[root@itoracle functions]# cargo run
Compiling functions v0.1.0 (/usr/local/automng/src/rust/test/functions)
Finished dev [unoptimized + debuginfo] target(s) in .80s
Running `target/debug/functions`
thread 'main' panicked at 'assertion failed: `(left == right)`
left: ``,
right: ``', src/main.rs:12:5
note: Run with `RUST_BACKTRACE=` for a backtrace.
while循环
fn main() {
let mut number = ;
while number != {
println!("{}!", number);
number = number - ;
}
println!("LIFTOFF!!!");
}
fn main() {
let a = [, , , , ];
let mut index = ;
while index < {
println!("the value is: {}", a[index]);
index = index + ;
}
}
for循环
相比上面while的方法,for不用关心数组的长度
fn main() {
let a = [, , , , ];
for element in a.iter() {
println!("the value is: {}", element);
}
}
for数字遍历
fn main() {
for number in (..).rev() {
println!("{}!", number);
}
println!("LIFTOFF!!!");
}
[root@itoracle functions]# cargo run
Compiling functions v0.1.0 (/usr/local/automng/src/rust/test/functions)
Finished dev [unoptimized + debuginfo] target(s) in .62s
Running `target/debug/functions`
!
!
!
LIFTOFF!!!
fn main() {
for number in .. {
println!("{}!", number);
}
println!("LIFTOFF!!!");
}
[root@itoracle functions]# cargo run
Compiling functions v0.1.0 (/usr/local/automng/src/rust/test/functions)
Finished dev [unoptimized + debuginfo] target(s) in .09s
Running `target/debug/functions`
!
!
!
LIFTOFF!!!
2.1 Rust概念的更多相关文章
- Rust入坑指南:核心概念
如果说前面的坑我们一直在用小铲子挖的话,那么今天的坑就是用挖掘机挖的. 今天要介绍的是Rust的一个核心概念:Ownership.全文将分为什么是Ownership以及Ownership的传递类型两部 ...
- Rust语言的多线程编程
我写这篇短文的时候,正值Rust1.0发布不久,严格来说这是一门兼具C语言的执行效率和Java的开发效率的强大语言,它的所有权机制竟然让你无法写出线程不安全的代码,它是一门可以用来写操作系统的系统级语 ...
- Rust初步(四):在rust中处理时间
这个看起来是一个很小的问题,我们如果是在.NET里面的话,很简单地可以直接使用System.DateTime.Now获取到当前时间,还可以进行各种不同的计算或者输出.但是这样一个问题,在rust里面, ...
- Rust入门篇 (1)
Rust入门篇 声明: 本文是在参考 The Rust Programming Language 和 Rust官方教程 中文版 写的. 个人学习用 再PS. 目录这东东果然是必须的... 找个时间生成 ...
- Rust语言:安全地并发
http://www.csdn.net/article/2014-02-26/2818556-Rust http://www.zhihu.com/question/20032903 Rust是近两年M ...
- A First Look at Rust Language
文 Akisann@CNblogs / zhaihj@Github 本篇文章同时发布在Github上:http://zhaihj.github.io/a-first-look-at-rust.html ...
- bloom-server 基于 rust 编写的 rest api cache 中间件
bloom-server 基于 rust 编写的 rest api cache 中间件,他位于lb 与api worker 之间,使用redis 作为缓存内容存储, 我们需要做的就是配置proxy,同 ...
- 【转】对 Rust 语言的分析
对 Rust 语言的分析 Rust 是一门最近比较热的语言,有很多人问过我对 Rust 的看法.由于我本人是一个语言专家,实现过几乎所有的语言特性,所以我不认为任何一种语言是新的.任何“新语言”对我来 ...
- Tokio,Rust异步编程实践之路
缘起 在许多编程语言里,我们都非常乐于去研究在这个语言中所使用的异步网络编程的框架,比如说Python的 Gevent.asyncio,Nginx 和 OpenResty,Go 等,今年年初我开始接触 ...
随机推荐
- C++获取电脑上连接的多个摄像头名称与编号
#include<iostream>#include "strmif.h"#include <initguid.h>#include<vector&g ...
- 业务逻辑:五、完成认证用户的动态授权功能 六、完成Shiro整合Ehcache缓存权限数据
一. 完成认证用户的动态授权功能 提示:根据当前认证用户查询数据库,获取其对应的权限,为其授权 操作步骤: 在realm的授权方法中通过使用principals对象获取到当前登录用户 创建一个授权信息 ...
- 2用java代码实现冒泡排序算法(转载)
import java.util.Scanner; public class Maopao { public static void main(String[] args) { System.out. ...
- bzoj4391 [Usaco2015 dec]High Card Low Card
传送门 分析 神奇的贪心,令f[i]表示前i个每次都出比对方稍微大一点的牌最多能赢几次 g[i]表示从i-n中每次出比对方稍微小一点的牌最多赢几次 ans=max(f[i]+g[i+1]) 0< ...
- Swing窗口Linux下不支持最大化问题
Swing窗口Linux下不支持最大化问题 摘自:https://www.linuxidc.com/Linux/2009-06/20519.htm [日期:2009-06-17] 来源:www.qua ...
- svn冲突问题详解 SVN版本冲突解决详解
svn冲突问题详解 SVN版本冲突解决详解 (摘自西西软件园,原文链接http://www.cr173.com/html/46224_1.html) 解决版本冲突的命令.在冲突解决之后,需要使用svn ...
- Android中常见的内存泄漏
为什么会产生内存泄漏? 当一个对象已经不需要再使用了,本该被回收时,而有另外一个正在使用的对象持有它的引用从而导致它不能被回收,这导致本该被回收的对象不能被回收而停留在堆内存中,这就产生了内存泄漏. ...
- 事件Event 介绍总结
最近在总结一些基础的东西,主要是学起来很难懂,但是在日常又有可能会经常用到的东西.前面介绍了 C# 的 AutoResetEvent的使用介绍, 这次介绍事件(event). 事件(event),对于 ...
- 正经学C#_变量与其数据类型:《c#入门经典》
这一篇总结以下变量与其数据类型. 变量:在c#中指 某一个值或者数据存储在变量中,并且可以取出或者查看.变量不仅仅是一种,也有很多种,细分而言就是类型.泛指就是变量.如果是要是使用变量就要 声明变量, ...
- ubuntu - 14.04,如何操作Gnome的任务栏?
搜索到的答案: in gnome classic you must press both the Alt & Super keys at the same time while right-c ...