Rust Notes & Tips

2019/07/20 Rust

精通x语言, 精通rust😂(最后更新于20200624)

开发环境搭建

  • Rust Forge, Other Rust Installation Methods, Other ways to install rustup上选择x86_64-unknown-linux-gnu 下载后赋权执行rustup-init(不要使用rust单独安装, 否则rustup无法单独安装, 要使用时仍需卸载rust进行重装)

  • 中国镜像及环境设置:参考如何利用科大源提速Cargo和Rust

  • rust-crates registry.index will be removed编译warning问题可参考https://lug.ustc.edu.cn/wiki/mirrors/help/rust-crates做如下设置(上一篇文档信息可能老了)

    更改 $HOME/.cargo/config 为以下内容:

    [source.crates-io]
    registry = "https://github.com/rust-lang/crates.io-index"
    replace-with = 'ustc'
    [source.ustc]
    registry = "git://mirrors.ustc.edu.cn/crates.io-index"
    
  • cargo new hello_world –bin

  • rust code formater: rustup component add rustfmt cargo fmt --all

  • use offline document: rustup doc --std

  • cargo fmt & cargo run (do format in background )

  • cargo new helloworld –bin

总结

default data typef64/i32

functions

fn by_ref(x: &i32) -> f64 { *x + 1;return 1.0;
}

reference & deference

let var: () =

If you are curious about the actual types of these variables, here is a useful trick. Just declare a variable with an explicit type which you know will be wrong:

Error

  • 自己把 use std::f64::consts;写成了using std::f64::consts;编译器的报错是

    error: expected one of `!` or `::`, found `std`
     --> src/main.rs:1:7
      |
    1 | using std::f64::consts;
      |       ^
    ^^ expected one of ! or :: here
    

    直接Google也没找到, 后来还是看文档才发现

Readlist

Reference

  • Rust for Linux, Rust中文社区

  • Compile time function execution

  • Zero Sized Types (ZSTs)

  • https://stevedonovan.github.io/rust-gentle-intro/1-basics.html

    • panic, segfault, garbage collection

      The first C program I wrote (on an DOS PC) took out the whole computer. Unix systems always behaved better, and only the process died with a segfault. Why is this worse than a Rust (or Go) program panicking? Because a panic happens when the original problem happens, not when the program has become hopelessly confused and eaten all your homework. Panics are memory safebecause they happen before any illegal access to memory. This is a common cause of security problems in C, because all memory accesses are unsafe and a cunning attacker can exploit this weakness.

      Panicking sounds desperate and unplanned, but Rust panics are structured - the stack is unwoundjust as with exceptions. All allocated objects are dropped, and a backtrace is generated.

      The downsides of garbage collection? The first is that it is wasteful of memory, which matters in those small embedded microchips which increasingly rule our world. The second is that it will decide, at the worst possible time, that a clean up must happen now. (The Mom analogy is that she wants to clean your room when you are at a delicate stage with a new lover). Those embedded systems need to respond to things when they happen (‘real-time’) and can’t tolerate unscheduled outbreaks of cleaning. Roberto Ierusalimschy, the chief designer of Lua (one of the most elegant dynamic languages ever) said that he would not like to fly on an airplane that relied on garbage-collected software.

    • iterate over an array

      In fact, it is more efficient to iterate over an array or slice this way than to use for i in 0..slice.len() {} because Rust does not have to obsessively check every index operation.

Search

    Categories Cloud

    Life Linux C/CPP Database Web Benchmarks Software Data Python TCP/IP Financial Stock Bug Golang Rust General Infrastructure TODO Movie Multitenancy Java Ant Algorithm Fastjson Death Build Deploy Education India Aamir Khan Society Female Learning Method OJ Interviewee Interviewer AVL Tree MyBatis Code Reading Design Diary Dating Heap Data Structure Summary Reading Love Claire Mcfall Ferryman Zodiac Astrology Chinese Calculator flink Dubbo docker redis

    Table of Contents