Download csv_datasets/7-Rust.csv from SynastriaNetworks/Projeto-Blitz: direct link, hf CLI and curl.
- Browser
- Download file 3.86 kB
-
https://huggingface.co/datasets/SynastriaNetworks/Projeto-Blitz/resolve/main/csv_datasets/7-Rust.csv
- Command line
-
hf download hf://datasets/SynastriaNetworks/Projeto-Blitz/csv_datasets/7-Rust.csv
-
curl -L -o 7-Rust.csv https://huggingface.co/datasets/SynastriaNetworks/Projeto-Blitz/resolve/main/csv_datasets/7-Rust.csv
3.86 kB
| "id","perguntas","saidas" | |
| "1","Como declaro uma função em Rust?","```rust | |
| fn soma(a: i32, b: i32) -> i32 { | |
| a + b | |
| } | |
| fn main() { | |
| println!(""{}"", soma(3, 4)); // 7 | |
| } | |
| ``` | |
| Em Rust, a última expressão sem `;` é o retorno. `i32` é o tipo inteiro de 32 bits. `println!` é uma macro, não uma função." | |
| "2","Como criar uma struct em Rust?","```rust | |
| struct Pessoa { | |
| nome: String, | |
| idade: u32, | |
| } | |
| impl Pessoa { | |
| fn new(nome: String, idade: u32) -> Self { | |
| Pessoa { nome, idade } | |
| } | |
| fn apresentar(&self) -> String { | |
| format!(""Olá, sou {} e tenho {} anos"", self.nome, self.idade) | |
| } | |
| } | |
| fn main() { | |
| let p = Pessoa::new(String::from(""Ana""), 25); | |
| println!(""{}"", p.apresentar()); | |
| } | |
| ``` | |
| `impl` bloco define métodos da struct. `&self` é uma referência imutável à instância." | |
| "3","Como trato erros em Rust com Result?","```rust | |
| use std::fs::File; | |
| use std::io::Read; | |
| fn ler_arquivo(caminho: &str) -> Result<String, std::io::Error> { | |
| let mut f = File::open(caminho)?; | |
| let mut conteudo = String::new(); | |
| f.read_to_string(&mut conteudo)?; | |
| Ok(conteudo) | |
| } | |
| fn main() { | |
| match ler_arquivo(""dados.txt"") { | |
| Ok(c) => println!(""{}"", c), | |
| Err(e) => eprintln!(""Erro: {}"", e), | |
| } | |
| } | |
| ``` | |
| `?` propaga o erro para o caller. `Result<T, E>` é o tipo padrão para operações que podem falhar." | |
| "4","Como faço um loop em Rust?","```rust | |
| // Loop for | |
| for i in 0..5 { | |
| println!(""{}"", i); | |
| } | |
| // Loop while | |
| let mut n = 0; | |
| while n < 3 { | |
| println!(""{}"", n); | |
| n += 1; | |
| } | |
| // Loop infinito com break | |
| let mut x = 0; | |
| loop { | |
| if x >= 10 { break; } | |
| x += 1; | |
| } | |
| ``` | |
| `0..5` é um range exclusivo (vai até 4). `0..=5` é inclusivo. `loop` roda até um `break`." | |
| "5","Como uso ownership e borrowing em Rust?","```rust | |
| fn main() { | |
| let s1 = String::from(""Olá""); | |
| let s2 = s1.clone(); // clona o conteúdo | |
| println!(""{} {}"", s1, s2); | |
| // Borrowing (empréstimo) | |
| fn tamanho(s: &String) -> usize { | |
| s.len() | |
| } | |
| let s3 = String::from(""Rust""); | |
| println!(""{}"", tamanho(&s3)); | |
| } | |
| ``` | |
| Por padrão, atribar move a ownership. Use `&` para emprestar sem transferir ownership. `.clone()` duplica dados." | |
| "6","Como trabalho com vectors em Rust?","```rust | |
| let mut nums: Vec<i32> = Vec::new(); | |
| nums.push(1); | |
| nums.push(2); | |
| nums.push(3); | |
| // Inicialização com valores | |
| let frutas = vec![String::from(""maçã""), String::from(""uva"")]; | |
| // Iteração | |
| for n in &nums { | |
| println!(""{}"", n); | |
| } | |
| // Acesso | |
| println!(""{}"", nums[0]); // 1 | |
| println!(""{}"", nums.get(0).unwrap()); // 1 | |
| ``` | |
| `Vec<T>` é o array dinâmico de Rust. `.get()` retorna `Option<&T>`, seguro contra access out-of-bounds." | |
| "7","Como criar enums com variantes em Rust?","```rust | |
| enum Forma { | |
| Circulo(f64), | |
| Retangulo(f64, f64), | |
| Quadrado(f64), | |
| } | |
| impl Forma { | |
| fn area(&self) -> f64 { | |
| match self { | |
| Forma::Circulo(r) => std::f64::consts::PI * r * r, | |
| Forma::Retangulo(l, a) => l * a, | |
| Forma::Quadrado(l) => l * l, | |
| } | |
| } | |
| } | |
| fn main() { | |
| let f = Forma::Circulo(2.0); | |
| println!(""Área: {:.2}"", f.area()); | |
| } | |
| ``` | |
| Enums em Rust são muito poderosos — variantes podem carregar dados. `match` faz pattern matching exaustivo." | |
| "8","Como criar e usar threads em Rust?","```rust | |
| use std::thread; | |
| use std::time::Duration; | |
| fn main() { | |
| let handle = thread::spawn(|| { | |
| for i in 1..5 { | |
| println!(""Thread: {}"", i); | |
| thread::sleep(Duration::from_millis(10)); | |
| } | |
| }); | |
| for i in 1..3 { | |
| println!(""Main: {}"", i); | |
| thread::sleep(Duration::from_millis(10)); | |
| } | |
| handle.join().unwrap(); // espera a thread terminar | |
| } | |
| ``` | |
| `thread::spawn` cria a thread; `.join()` bloqueia até terminar. Para compartilhar dados entre threads, use `Arc<Mutex<T>>`." | |