Skip to content

Instantly share code, notes, and snippets.

@eonil
Last active August 29, 2015 14:09
Show Gist options
  • Save eonil/1eb1fd993aad2f275f21 to your computer and use it in GitHub Desktop.
Save eonil/1eb1fd993aad2f275f21 to your computer and use it in GitHub Desktop.
Rust Notes

Rust Notes

This Gist is just a note of my current personal understanding, and does not define or claim the actual Rust behaviors. Always can be wrong, and may contain some errors. Do not rely on this information if you're learning Rust.

Good Reading Materials for Learning

Building from Source (0.13-dev)

Before you try this, please consider using nightly binaries for OSX.

git clone https://github.com/rust-lang/rust.git
./configure --prefix=$HOME/Unix/rust --local-rust-root=$HOME/Unix/rust
make -j7 && make -j7 install

This will take about half an hour. Adjust --prefix and --local-rust-root flags to where you want to install to. Adjust -j flag by number of your processor cores.

After doing this, calling rustc will spit an error that you need some dylib in some weird path. The path doesn't matter if you supply correct dylib. So, add these lines to you .profile (OSX) or any other login script.

# Rust support.
export PATH="$PATH:$HOME/Unix/rust/bin"
export DYLD_LIBRARY_PATH="$HOME/Unix/rust/lib"
  • I though this will install cargo, but it didn't. Cargo need to be installed separately and manually.

Building Cargo

Building from source doesn't work. I don't know why.

rustc 0.13.0-nightly (5484d6f6d 2014-12-02 00:22:00 +0000)
target/snapshot/bin/cargo build --target x86_64-apple-darwin  
    Updating git repository `https://github.com/rust-lang/glob`
    Updating git repository `https://github.com/docopt/docopt.rs`
    Updating git repository `https://github.com/alexcrichton/flate2-rs`
    Updating git repository `https://github.com/carllerche/hamcrest-rust.git`
    Updating git repository `https://github.com/alexcrichton/git2-rs`
    Updating git repository `https://github.com/alexcrichton/toml-rs`
    Updating git repository `https://github.com/alexcrichton/tar-rs`
    Updating git repository `https://github.com/servo/rust-url`
    Updating git repository `https://github.com/rust-lang/time`
Unable to update https://github.com/carllerche/curl-rust#5d0f5c88

Just use nightly binary for OSX.

Racer

Currently Racer needs RUST_SRC_PATH to be set to source code directory of Rust compiler. This doesn't mean your project source directory. Just take care on this.

Compile single file

rustc main.rs

Rust can compile only one file at once. One file will become a crate. You need to link crates later.

Compiling linkabe crate (library)

rustc x1.rs

It's same. Though compiler provides overriding switch, but it's better to note the type of crate in the source file.

#![crate_type = "lib"]

Use this for executables.

#![crate_type = "bin"]

Source

Linking multiple crates

rustc x1.rs
rustc -L . main.rs
./main

Links library crate x1 into executable crate main. rustc does not lookup library sources automatically. You need to compile all the library sources yourself. Anyway, an executable can have only one entry point, so there can be only one main rust file, so you can treat everything else as just all libraries.

Referencing functions in another crates

Declare extern and use.

extern crate x1;

fn main() {
	x1::f1();
	println!("main!");
}

Source

Emitting debug informations (dSYM)

Use -g flag.

rustc -g x1.rs
rustc -g -L . main.rs
./main

Debugging with LLDB

lldb ./main     # Launch LLDB with compiled program. Don't forget to emit dSYM.
b main          # Set breakpoint at function `main`.
run             # Start running. Program will stop at the `main` function.
n               # Step to next line.
n
n

See LLDB manual for commands.

Compiler Options (rustc, 1.0.0-alpha)

Codegen parameters.

Eonil$ rustc -C help

Available codegen options:

    -C                    ar=val -- tool to assemble archives with
    -C                linker=val -- system linker to link outputs with
    -C             link-args=val -- extra arguments to pass to the linker (space separated)
    -C                       lto -- perform LLVM link-time optimizations
    -C            target-cpu=val -- select target processor (llc -mcpu=help for details)
    -C        target-feature=val -- target specific attributes (llc -mattr=help for details)
    -C                passes=val -- a list of extra LLVM passes to run (space separated)
    -C             llvm-args=val -- a list of arguments to pass to llvm (space separated)
    -C                save-temps -- save all temporary output files during compilation
    -C                     rpath -- set rpath values in libs/exes
    -C     no-prepopulate-passes -- don't pre-populate the pass manager with a list of passes
    -C        no-vectorize-loops -- don't run the loop vectorization optimization passes
    -C          no-vectorize-slp -- don't run LLVM's SLP vectorization pass
    -C                soft-float -- generate software floating point library calls
    -C            prefer-dynamic -- prefer dynamic linking to static linking
    -C          no-integrated-as -- use an external assembler rather than LLVM's integrated one
    -C                no-redzone -- disable the use of the redzone
    -C      relocation-model=val -- choose the relocation model to use (llc -relocation-model for details)
    -C            code-model=val -- choose the code model to use (llc -code-model for details)
    -C              metadata=val -- metadata to mangle symbol names with
    -C        extra-filename=val -- extra data to put in each output filename
    -C         codegen-units=val -- divide crate into N units to optimize in parallel
    -C                remark=val -- print remarks for these optimization passes (space separated, or "all")
    -C            no-stack-check -- disable checks for stack exhaustion (a memory-safety hazard!)
    -C             debuginfo=val -- debug info emission level, 0 = no debug info, 1 = line tables only, 2 = full debug info with variable and type information
    -C             opt-level=val -- Optimize with possible levels 0-3

LINT parameters.

Eonil$ rustc -W help

Available lint options:
    -W <foo>           Warn about <foo>
    -A <foo>           Allow <foo>
    -D <foo>           Deny <foo>
    -F <foo>           Forbid <foo> (deny, and deny all overrides)


Lint checks provided by rustc:

                            name  default  meaning
                            ----  -------  -------
                    box-pointers  allow    use of owned (Box type) heap memory
              fat-ptr-transmutes  allow    detects transmutes of fat pointers
                    missing-docs  allow    detects missing documentation for public members
                   unsafe-blocks  allow    usage of an `unsafe` block
               unstable-features  allow    enabling unstable features
            unused-extern-crates  allow    extern crates that are never used
            unused-import-braces  allow    unnecessary braces around an imported item
           unused-qualifications  allow    detects unnecessarily qualified names
                  unused-results  allow    unused result of an expression in a statement
                unused-typecasts  allow    detects unnecessary type casts that can be removed
        variant-size-differences  allow    detects enums with widely varying variant sizes
                       dead-code  warn     detect unused, unexported items
                      deprecated  warn     detects use of #[deprecated] items
                 improper-ctypes  warn     proper use of libc types in foreign modules
    missing-copy-implementations  warn     detects potentially-forgotten implementations of `Copy`
            non-camel-case-types  warn     types, variants, traits and type parameters should have camel case names
    non-shorthand-field-patterns  warn     using `Struct { x: x }` instead of `Struct { x }`
                  non-snake-case  warn     methods, functions, lifetime parameters and modules should have snake case names
          non-upper-case-globals  warn     static constants should have uppercase identifiers
            overflowing-literals  warn     literal out of range for its type
                 path-statements  warn     path statements with no effect
              raw-pointer-derive  warn     uses of #[derive] with raw pointers are rarely correct
                   unknown-lints  warn     unrecognized lint attribute
                unreachable-code  warn     detects unreachable code paths
               unsigned-negation  warn     using an unary minus operator on unsigned type
                        unstable  warn     detects use of #[unstable] items (incl. items with no stability attribute)
               unused-allocation  warn     detects unnecessary allocations that can be eliminated
              unused-assignments  warn     detect assignments that will never be read
               unused-attributes  warn     detects attributes that were not used by the compiler
              unused-comparisons  warn     comparisons made useless by limits of the types involved
                  unused-imports  warn     imports that are never used
                 unused-must-use  warn     unused result of a type flagged as #[must_use]
                      unused-mut  warn     detect mut variables which don't need to be mutable
                   unused-parens  warn     `if`, `match`, `while` and `return` do not need parentheses
                   unused-unsafe  warn     unnecessary use of an `unsafe` block
                unused-variables  warn     detect variables which are not used in any way
                        warnings  warn     mass-change the level for lints which produce warnings
                      while-true  warn     suggest using `loop { }` instead of `while true { }`
             exceeding-bitshifts  deny     shift exceeds the type's number of bits
             unknown-crate-types  deny     unknown crate type found in #[crate_type] directive
                unknown-features  deny     unknown features found in crate-level #[feature] directives


Lint groups provided by rustc:

         name  sub-lints
         ----  ---------
    bad-style  non-camel-case-types, non-snake-case, non-upper-case-globals
       unused  unused-imports, unused-variables, unused-assignments, dead-code, unused-mut, unreachable-code, unused-must-use, unused-unsafe, path-statements


Compiler plugins can provide additional lints and lint groups. To see a listing of these, re-run `rustc -W help` with a crate filename.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment