赞
踩
Rust是一门赋予每个人构建可靠且高效软件能力的编程语言。可靠主要体现在安全性上。其高效不仅限于开发效率,它的执行效率也是令人称赞的,是一种少有的兼顾开发效率和执行效率的语言。Rust 语言由 Mozilla 开发,最早发布于 2014 年 9 月。Rust 的编译器是在 MIT License 和 Apache License 2.0 双重协议声明下的免费开源软件。
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传
高性能:Rust 速度惊人且内存利用率极高。由于没有运行时和垃圾回收,它能够胜任对性能要求特别高的服务,可以在嵌入式设备上运行,还能轻松和其他语言集成。
可靠性:Rust 丰富的类型系统和所有权模型保证了内存安全和线程安全,让您在编译期就能够消除各种各样的错误。
生产力:Rust 拥有出色的文档、友好的编译器和清晰的错误提示信息, 还集成了一流的工具——包管理器和构建工具, 智能地自动补全和类型检验的多编辑器支持, 以及自动格式化代码等等。
Rustacean:使用 rust 的攻城狮不叫 ruster 而是叫 Rustacean ,咱也不知道为什么,书上就是这么说的。
以 windows 11 为例
下载 rustup-init.exe ,双击此可执行程序会打开一个命令行程序,此程序引导安装,具体安装过程:
Rust Visual C++ prerequisites Rust requires the Microsoft C++ build tools for Visual Studio 2013 or later, but they don't seem to be installed. The easiest way to acquire the build tools is by installing Microsoft Visual C++ Build Tools 2019 which provides just the Visual C++ build tools: https://visualstudio.microsoft.com/visual-cpp-build-tools/ Please ensure the Windows 10 SDK and the English language pack components are included when installing the Visual C++ Build Tools. Alternately, you can install Visual Studio 2019, Visual Studio 2017, Visual Studio 2015, or Visual Studio 2013 and during install select the "C++ tools": https://visualstudio.microsoft.com/downloads/ Install the C++ build tools before proceeding. If you will be targeting the GNU ABI or otherwise know what you are doing then it is fine to continue installation without the build tools, but otherwise, install the C++ build tools before proceeding. Continue? (y/N) y Welcome to Rust! This will download and install the official compiler for the Rust programming language, and its package manager, Cargo. Rustup metadata and toolchains will be installed into the Rustup home directory, located at: C:\Users\cml\.rustup This can be modified with the RUSTUP_HOME environment variable. The Cargo home directory located at: C:\Users\cml\.cargo This can be modified with the CARGO_HOME environment variable. The cargo, rustc, rustup and other commands will be added to Cargo's bin directory, located at: C:\Users\cml\.cargo\bin This path will then be added to your PATH environment variable by modifying the HKEY_CURRENT_USER/Environment/PATH registry key. You can uninstall at any time with rustup self uninstall and these changes will be reverted. Current installation options: default host triple: x86_64-pc-windows-msvc default toolchain: stable (default) profile: default modify PATH variable: yes 1) Proceed with installation (default) 2) Customize installation 3) Cancel installation > info: profile set to 'default' info: default host triple is x86_64-pc-windows-msvc info: syncing channel updates for 'stable-x86_64-pc-windows-msvc' info: latest update on 2022-01-20, rust version 1.58.1 (db9d1b20b 2022-01-20) info: downloading component 'cargo' 3.8 MiB / 3.8 MiB (100 %) 1.7 MiB/s in 2s ETA: 0s info: downloading component 'clippy' 1.6 MiB / 1.6 MiB (100 %) 1.5 MiB/s in 1s ETA: 0s info: downloading component 'rust-docs' 18.8 MiB / 18.8 MiB (100 %) 3.3 MiB/s in 5s ETA: 0s info: downloading component 'rust-std' 22.9 MiB / 22.9 MiB (100 %) 3.2 MiB/s in 7s ETA: 0s info: downloading component 'rustc' 65.2 MiB / 65.2 MiB (100 %) 493.2 KiB/s in 1m 14s ETA: 0s info: downloading component 'rustfmt' 2.2 MiB / 2.2 MiB (100 %) 631.2 KiB/s in 3s ETA: 0s info: installing component 'cargo' info: installing component 'clippy' info: installing component 'rust-docs' 18.8 MiB / 18.8 MiB (100 %) 1.9 MiB/s in 6s ETA: 0s info: installing component 'rust-std' 22.9 MiB / 22.9 MiB (100 %) 10.5 MiB/s in 2s ETA: 0s info: installing component 'rustc' 65.2 MiB / 65.2 MiB (100 %) 12.2 MiB/s in 5s ETA: 0s info: installing component 'rustfmt' info: default toolchain set to 'stable-x86_64-pc-windows-msvc' stable-x86_64-pc-windows-msvc installed - rustc 1.58.1 (db9d1b20b 2022-01-20) Rust is installed now. Great! To get started you may need to restart your current shell. This would reload its PATH environment variable to include Cargo's bin directory (%USERPROFILE%\.cargo\bin). Press the Enter key to continue.
命令 | 说明 | 备注 |
---|---|---|
rustup doc | 打开官方指导文档 | |
cargo new projectName | 创建一个rust工程 | 示例:cargo new firstRustProject |
cargo run | 运行rust工程 | |
cargo build | 编译rust工程 | 若增加了依赖,即修改了toml文件,需要重新编译 |
示例:
fn main() { println!("Hello, world!"); //变量默认是不可变的,加上 mut 关键字就可以重新赋值。 let mut x=5; println!("The value of x is {} ",x); x=6; println!("The value of x is {} ",x); //变量的隐藏 let money=100; println!("money is {}",money); let money =money+8; println!("money is {}",money); let money="一百元"; println!("money is {}",money); //常量使用 const 关键字声明,声明的时候必须指定数据类型,常量名全大写。 //不需要let , 不可使用mut 修饰 const MAX_PIONTS: u32=888; println!("The constant is {}",MAX_PIONTS); let result:char= a_function(88, 'M', false); println!("result is {}",result); } fn a_function(a:u64,b:char,c:bool)-> char{ println!("a is {}",a); println!("b is {}",b); println!("c is {}",c); return 'N'; }
输出:
Hello, world!
The value of x is 5
The value of x is 6
money is 100
money is 108
money is 一百元
The constant is 888
a is 88
b is M
c is false
result is N
整数,浮点,布尔,字符
可以将多个值放到一个数据类型中。
fn main() {
println!("Hello, world!");
let q=3.0;
let q:f32=5.00;
let w=true;
let r:bool =false;
let t='声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/从前慢现在也慢/article/detail/556622
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。