美文网首页
rust 开篇

rust 开篇

作者: xiongzenghui | 来源:发表于2018-08-12 17:41 被阅读100次

1、rust 语言特点

  • Rust 基于C++,理论上来说,它的速度与 C / C++ 同级
  • Rust 被设计为保证内存和线程安全
  • Rust 类似 C 或者 C++ 也是编译型的编程语言
  • 与C/C++区别
    • Rust是线程安全的
    • Rust 不允许空指针悬挂指针
  • Rust主要用于高度安全和并发的软件系统

2、下载安装rust

1. 源码安装 rust

1. 下载rust源码

git clone

git clone https://github.com/rust-lang/rust.git

或者直接下载当前版本的源码压缩包

wget https://github.com/rust-lang/rust/archive/master.zip

2. rust源码编译的环境要求

  • g++ 4.7 or later or clang++ 3.x or later
  • python 2.7 (but not 3.x)
  • GNU make 3.81 or later
  • cmake 3.4.3 or later
  • curl
  • git

3. 进入到rust源码目录编译

➜  rust ./x.py build && sudo ./x.py install
  • ./x.py build 执行py脚本下载一些依赖文件
  • sudo ./x.py install 执行py脚本进行安装

2. 直接下载安装

➜  ~ curl -sSf https://static.rust-lang.org/rustup.sh | sh
Password:
rustup: gpg available. signatures will be verified
rustup: downloading manifest for 'stable'
rustup: downloading toolchain for 'stable'
######################################################################## 100.0%
gpg: assuming signed data in '/Users/xiongzenghui/.rustup.sh/dl/279d7075ba297b992559/rust-1.24.1-x86_64-apple-darwin.tar.gz'
gpg: 签名建立于 五  3/ 2 07:47:10 2018 CST
gpg:               使用 RSA 密钥 5CB4A9347B3B09DC
gpg: 完好的签名,来自于“Rust Language (Tag and Release Signing Key) <rust-key@rust-lang.org>” [未知]
gpg: 警告:这把密钥未经受信任的签名认证!
gpg:       没有证据表明这个签名属于它所声称的持有者。
主钥指纹: 108F 6620 5EAE B0AA A8DD  5E1C 85AB 96E6 FA1B E5FE
子钥指纹: C134 66B7 E169 A085 1886  3216 5CB4 A934 7B3B 09DC
rustup: installing toolchain for 'stable'
rustup: extracting installer
Password:
install: creating uninstall script at /usr/local/lib/rustlib/uninstall.sh
install: installing component 'rustc'
install: installing component 'cargo'
install: installing component 'rls-preview'
install: installing component 'rustfmt-preview'
install: installing component 'rust-analysis-x86_64-apple-darwin'
install: installing component 'rust-std-x86_64-apple-darwin'
install: installing component 'rust-docs'

    Rust is ready to roll.

➜  ~

3、rust helloworld

源文件

fn main() {
  println!("Hello World by Rust ~");
}

编译链接生成可执行程序main

➜  main rustc main.rs
➜  main file main
main: Mach-O 64-bit executable x86_64
➜  main

执行可执行程序main

➜  main ./main
Hello World by Rust ~
➜  main

4、格式化输出

// fn main() {
//   let mystr :&str = "abcd";
//   println!("mystr  address:  {:p}", &mystr);
//   println!("mystr  address:  {:p}", &mystr[1..]);
//   println!("mystr  address:  {:p}", &mystr[2..]);
//   println!("mystr  address:  {:p}", &mystr[3..]);
// }


fn main() {
  // {}表示占位符,由后面的31替换
  println!("{} days", 31);

  // {0} 使用后面第几个参数替换
  println!("{0}, this is {1}. {1}, this is {0}", "Alice", "Bob");

  // named arguments.
  println!("{subject} {verb} {predicate}",
    predicate="over the lazy dog",
    subject="the quick brown fox",
    verb="jumps"
  );

  // Special formatting can be specified after a `:`.
  // :b => 二进制输出
  println!("{} - {:b} ", 2, 2);

  // error 少一个参数
  // println!("My name is {0}, {1} {0}", "Bond")

  // {:?} 自动根据数据类型输出
  let f2: bool = false;
  println!("f2 = {:?}", f2);

  // {:p} 输出变量标记的内存地址
  let name = "xiong";
  println!("&name = {:p}", &name) 
}
➜  main make
rustc main.rs
./main
31 days
Alice, this is Bob. Bob, this is Alice
the quick brown fox jumps over the lazy dog
2 - 10
f2 = false
&name = 0x7fff58cf2740
➜  main

5、条件编译

1. #[cfg(foo)]

#[cfg(foo)]
fn foo() {
  println!("foo()")
}

fn main()
{
  #[cfg(foo)]
  foo();
}
➜  main rustc main.rs
➜  main ./main
➜  main
➜  main
➜  main rustc main.rs --cfg foo
➜  main ./main
foo()
➜  main

2. #[cfg(not(foo))]

#[cfg(not(foo))]
fn foo() {
  println!("foo()")
}

fn main()
{
  #[cfg(not(foo))]
  foo();
}
➜  main rustc main.rs --cfg foo
➜  main ./main
➜  main
➜  main rustc main.rs
➜  main ./main
foo()
➜  main

3. [cfg(foo = "baz")]

#[cfg(feature = "run")]
fn run() {
  println!("run()")
}

#[cfg(feature = "cry")]
fn cry() {
  println!("cry()")
}

fn main()
{
  #[cfg(feature = "run")]
  run();

  #[cfg(feature = "cry")]
  cry();
}
➜  main rustc main.rs --cfg feature=\"run\"
➜  main ./main
run()
➜  main
➜  main rustc main.rs --cfg feature=\"cry\"
➜  main ./main
cry()
➜  main
➜  main rustc main.rs
➜  main ./main
➜  main

4. any 条件组合

#[cfg(all(run, cry))]
fn run() {
  println!("run()")
}

#[cfg(all(run, cry))]
fn cry() {
  println!("cry()")
}

#[cfg(any(run, cry))]
fn any() {
  println!("cry() || run()")
}

fn main()
{
  #[cfg(any(run, cry))]
  any();
}
➜  main rustc main.rs
➜  main ./main
➜  main
➜  main rustc main.rs --cfg run
➜  main ./main
cry() || run()
➜  main
➜  main rustc main.rs --cfg cry
➜  main ./main
cry() || run()
➜  main

5. all 条件组合

#[cfg(all(run, cry))]
fn run() {
  println!("run()")
}

#[cfg(all(run, cry))]
fn cry() {
  println!("cry()")
}

#[cfg(any(run, cry))]
fn any() {
  println!("cry() || run()")
}

fn main()
{
  #[cfg(all(run, cry))]
  {
    run();
    cry();
  }

  #[cfg(any(run, cry))]
  any()
}
➜  main rustc main.rs
➜  main ./main
➜  main
➜  main rustc main.rs --cfg run
➜  main ./main
cry() || run()
➜  main
➜  main rustc main.rs --cfg cry
➜  main ./main
cry() || run()
➜  main
➜  main rustc main.rs --cfg cry --cfg run
➜  main ./main
run()
cry()
cry() || run()
➜  main

6. 任意位置使用cfg内置对象

fn main()
{
  if cfg!(target_os = "macos") || cfg!(target_os = "ios") {
    println!("Think Different!");
  }
}
➜  main rustc main.rs
➜  main ./main
Think Different!
➜  main

相关文章

网友评论

      本文标题:rust 开篇

      本文链接:https://www.haomeiwen.com/subject/llasbftx.html