美文网首页
Rust学习笔记(1)

Rust学习笔记(1)

作者: 爱写作的harry | 来源:发表于2019-05-28 17:37 被阅读0次

内容整理自:https://doc.rust-lang.org/book/title-page.html

Chapter 1

  • check rust version
rust —version
  • How to run a rust program
fn main() {
    println!("hello world");
}
  • main function is the entry to run in every app
  • ! means it runs a macro instead of a function
  • 2 steps to run: compile and run
    • pros: run everywhere even without rust installed
    • cons: 2 steps
    • just a design tradeoff

Cargo

  • What is cargo
    • build system
    • package manager
  • already installed with rust
  • check cargo version
    cargo —version
  • Creating a project with Cargo
    cargo new hello_cargo
  • packages of code are referred to as crates
  • build with cargo
    cargo build
  • run
    ./target/build/hello_cargo
    Or
    cargo run
  • check package doc
    cargo doc --open
  • quickly checks your code to make sure it compiles but doesn’t produce an executable
    cargo check
    Faster than cargo build, can be used as check your work
  • release
    cargo build --release
    Build with optimizations, in target/release

Chapter 2 guessing game

  • variables
let foo = 5; // immutable
let mut bar = 5; // mutable
  • storing variables
io::stdin().read_line(&mut guess)
    .expect("Failed to read line");

The & indicates that this argument is a reference, which gives you a way to let multiple parts of your code access one piece of data without needing to copy that data into memory multiple times.

  • Handling Potential Failure with the Result Type
    io::stdin().read_line(&mut guess).expect("Failed to read line");
    The Result types are enumerations, often referred to as enums. An enumeration is a type that can have a fixed set of values, and those values are called the enum’s variants
  • print values
    println!("You guessed: {}", guess);
    {} is a placeholder

相关文章

  • Rust学习笔记(1)

    内容整理自:https://doc.rust-lang.org/book/title-page.html Chap...

  • Rust 学习笔记 1 - ownership 基础

    0. 对于一个常年写 Java 的程序员来说,Rust 最难入门的概念的应该是 ownership。 在 Java...

  • rust学习笔记

    Cargo:Rust 的构建工具和包管理器 您在安装 Rustup 时,也会安装 Rust 构建工具和包管理器的最...

  • 2.Rust新手教程-数据类型

    学习笔记,仅此而已 Rust是静态编译语言,在编译时必须知道所有变量的类型,其中Rust内部有2套机制 基于定义的...

  • Rust学习笔记-01

    0. Rust的源文件 像c语言的源文件我们以".c"为后缀,c++以".cpp"为后缀,java以".java"...

  • [学习笔记]初试rust

    什么是Rust? Rust 是一门系统级编程语言,可以被归纳为通用的、多范式、编译型语言,与C/C++不同的是,R...

  • Rust学习笔记(一)

    第一步要做的事情是 安装Rust编译环境(人人都有的windows) 咱工作机器是win7+mingw所以直接到下...

  • Rust 学习笔记 - 安装

    在 Linux 和 Mac 上安装 如果使用 Linux 或 Mac, 最简单的做法就是打开一个终端并输入: 这将...

  • Rust 学习笔记 - 函数

    Rust 是一门多范式的编程语言,但 Rust 的编程风格是更偏向于函数式的,函数在 Rust 中是“一等公民”。...

  • Rust学习笔记(3)

    内容整理自:https://doc.rust-lang.org/book/title-page.html Chap...

网友评论

      本文标题:Rust学习笔记(1)

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