CARVIEW |
Select Language
HTTP/2 200
date: Sun, 12 Oct 2025 18:44:58 GMT
content-type: text/html; charset=UTF-8
server: cloudflare
x-frame-options: DENY
x-content-type-options: nosniff
x-xss-protection: 1;mode=block
vary: accept-encoding
cf-cache-status: DYNAMIC
content-encoding: gzip
set-cookie: _csrf-frontend=67fc6527f9ffe557a4dcff33e9bb1d9ecf0952c95b04c26ccc3eb33f099c37c2a%3A2%3A%7Bi%3A0%3Bs%3A14%3A%22_csrf-frontend%22%3Bi%3A1%3Bs%3A32%3A%224jUk0-jF_YQ1eD3N-xr-vZ3txYykT09o%22%3B%7D; HttpOnly; Path=/
cf-ray: 98d8c066998e0bc9-BLR
Ejercicio Rust Write Read Struct Enum - Pastebin.com
SHARE
TWEET

Ejercicio Rust Write Read Struct Enum
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Ejercicio 92-93 Rust
- /*
- CONTENIDO DE Cargo.toml
- [package]
- name = "ejemplos"
- version = "0.1.0"
- edition = "2021"
- # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
- [dependencies]
- enum_str = "0.1.2"
- */
- #[allow(unused_imports)]
- #[warn(dead_code)]
- use std::fs::OpenOptions;
- use std::io::Write;
- use std::fs;
- //Una enumeracio de especialidades
- enum Esp {
- IT,RecursosHumanos,Marketing
- }
- //Estructura Empleado
- struct Empleado {
- nombre:String,
- edad:u32,
- especialidad:Esp
- }
- //Lee un fichero externo {nombre}
- fn lee (nombre:&String){
- let contenido=fs::read_to_string (nombre).expect ("Error");
- println! ("{}","\n");
- println! ("{}",contenido);
- }
- //Escribe en un fichero externo {nombre} los datos de una estructura
- fn escribe (nombre :&String,datos :&Empleado){
- let mut tmp:String=String::new();
- let mut fichero=OpenOptions::new()
- .create(true)
- .append(true)
- .open(nombre)
- .expect ("Error apertura ");
- tmp.push_str ( &(datos.nombre).to_string());
- tmp.push('\n');
- fichero.write_all (tmp.as_bytes()).expect ("Error escritura");
- tmp.push_str ( &((datos.edad).to_string()+"\n"));
- fichero.write_all (tmp.as_bytes()).expect ("Error escritura");
- match datos.especialidad{
- Esp::IT=>tmp.push_str ("IT"),
- Esp::RecursosHumanos => tmp.push_str ("Recursos Humanos"),
- Esp::Marketing =>tmp.push_str ( "Marketing"),
- }
- fichero.write_all (tmp.as_bytes()).expect ("Error escritura");
- }
- fn main (){
- //Nombre del fichero externo
- let fichero:String ="ficha.txt".to_string();
- //Crea un empleado con estructura Empleado
- let mut empleado = Empleado{
- nombre:"Tony".to_string(),
- edad:57,
- especialidad:Esp::IT
- };
- //Escribe este empleado
- escribe(&fichero ,&empleado);
- //Modifica el empleado
- empleado.nombre ="Icaro".to_string();
- empleado.edad =18;
- empleado.especialidad = Esp::RecursosHumanos;
- //Escribe
- escribe(&fichero,&empleado);
- //Modifica
- empleado.nombre ="F4LEC".to_string();
- empleado.edad=100;
- empleado.especialidad =Esp::Marketing;
- //Escribe
- escribe(&fichero,&empleado);
- //Lectura fichero externo
- lee (&fichero);
- }
Advertisement
Add Comment
Please, Sign In to add comment
-
🚨 Free Crypto Method 🚨
JavaScript | 0 sec ago | 0.24 KB
-
⭐⭐⭐MAKE $9OO INSTANTLY D G⭐⭐
Java | 4 sec ago | 0.10 KB
-
✅ Make $2500 in 20 minutes⭐ T
JavaScript | 5 sec ago | 0.25 KB
-
💡 EASY MONEY GUIDE ✅ Working
JavaScript | 10 sec ago | 0.24 KB
-
📌 Swapzone +37% glitch ⭐ 5
JavaScript | 15 sec ago | 0.25 KB
-
⭐⭐⭐Profit Method⭐⭐
Java | 15 sec ago | 0.10 KB
-
🔥 Exchange profit method
JavaScript | 19 sec ago | 0.24 KB
-
✅⭐ Make huge profits on trading ⭐⭐ 4
JavaScript | 25 sec ago | 0.25 KB
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand