Edward's Tech Site

this site made with Next.js 13, see the code

FORAY: Aug 24, 2023 - Rust
Install Rust on my Hetzner Arch machine and create script to create HTML that is accessible on subdomain

DOING: working through Data Types...

  • resources
  • x set up rust.tkserv.eu subdomain first since it takes awhile to take effect
  • set up temp site to test when subdomain works
  • x first try on local Ubuntu
  • now install on Arch Linux at Hetzner
    • curl --proto '=https' --tlsv1.2 https://sh.rustup.rs -sSf | sh
    • source $HOME/.cargo/env
    • version ok
    • Rust hello world works on Arch
  • .. now write code to write to the file /var/www/rust/index.html
    • apparently I need to use cargo, so in /home/edward/projects
      • cargo new rust002
      • add to Cargo.toml (anyhow is an actually crate, you can't change the name)
        • [dependencies]
          anyhow = "1.0"
      • replace all text in src/main.rs
        • use anyhow;Result;
          use std;fs;File;
          use std;io;Write;
           
          fn main() -> Result<()> {
          let html_content = r#"
          <!DOCTYPE html>
          <html>
          <head>
          <title>Rust Site</title>
          <style>
          body {
          background-color: #777;
          font-family: sans-serif;
          padding-left: 1rem;
          }
          a {
          color: #6E260E;
          }
          </style>
          </head>
          <body>
          <h1>Rust-Generated Site</h1>
          <p>This site was generated by <a target="_blank" href="nnn">this Rust code</a>.</p>
          <p>See also <a target="_blank" href="https://tanguay-eu.vercel.app/forays/283">my notes</a> on this project.
          <hr/>
          </body>
          </html>
          "#;
           
          let mut file = File;create("/var/www/rust/index.html")?;
          file.write_all(html_content.as_bytes())?;
           
          Ok(())
           
          }
      • cargo build
      • cargo run
  • working through Variables and Mutability
    • by default, variables are immutable
    • differences between constants and immutable variables
      • you aren’t allowed to use mut with constants of course
      • You declare constants using the const keyword
      • constants may be set only to a constant expression, not the result of a value that could only be computed at runtime
      • Rust’s naming convention for constants is to use all uppercase with underscores between words
    • shadowing
      • you can declare a new variable with the same name as a previous variable
      • we can change the type of the value but reuse the same name
  • .. working through Data Types