Introdução
- Instalando o Rust no linux
- Escrevendo o Hello World!
- Usando
cargo
(gerenciador de pacotes e sistema de build)
Instalando o Rust no linux
Devemos baixar o rustup
, que é responspável por baixar o rust
e fazer a instalação
curl --proto '=https' --tlsv1.2 https://sh.rustup.rs -sSf | sh
Algumas informações devem aparecer
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:/home/jao/Documents/Programs/rust/rustup/
This can be modified with the RUSTUP_HOME environment variable.
The Cargo home directory located at:
/home/jao/Documents/Programs/rust/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:/home/jao/.cargo/bin
This path will then be added to your PATH environment variable by
modifying the profile files located at:/home/jao/.profile /home/jao/.zshenv
You can uninstall at any time with rustup self uninstall and
these changes will be reverted.Current installation options:
default host triple: x86_64-unknown-linux-gnu
default toolchain: stable (default)
profile: default
modify PATH variable: yes1) Proceed with installation (default)
2) Customize installation
3) Cancel installation
Basta digitar 1 e a instalação irá começar.
Se tudo occorrer bem, a seguinte mensagem deve aparecer
Rust is installed now. Great!
To get started you may need to restart your current shell. This would reload your PATH environment variable to include Cargo's bin directory (/home/jao/Documents/Programs/rust/cargo//bin).
To configure your current shell, run: source /home/jao/Documents/Programs/rust/cargo//env
Alguns comandos úteis
- rustup update
- rustup self uninstall
- rustc --version
- rustup doc
Escrevendo o Hello World!
Crie um arquivo chamado main.rs
e adicione o conteúdo
fn main() {
println!("Hello World!");
}
Para compilar e executar, basta digitar no terminal
rustc main.rc
./main
O resultado será o texto Hello World!
Alguns comandos úteis
- rustfmt (para formatar o código)
Usando cargo (gerenciador de pacotes e sistema de build)
Para criar um projeto utilizando o cargo
, basta usar o comando
cargo new hello_cargo
Um arquivo main.rs
será criado dentro da pasta hello_cargo/src/
. Para compilar o código, basta digitar
cargo build
dentro da pasta hello_cargo, a pasta target/
será criada e o executável está em hello_cargo/target/debug
, chamado de hello_cargo
Comandos úteis
- cargo run (para executar o programa)
- cargo check (para fazer as checagens no código, sem compilar)
- cargo build --release (para fazer uma build de release, por padrão a build é de debug).