2
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Rust 1.80.0の機能を使ってみる

Posted at

はじめに

Rust触ってますアピールのための記事です。Announcing Rust 1.80.0の内容をそのまま使うだけです。
Rustの1.80.0の新機能を知りたい方は本家のAnnouncing Rust 1.80.0をお勧めします。

Rust 1.80.0の新機能

  • LazyCell, LazyLockの追加
  • cfgの名前
  • Rangeをいい感じに拡張

LazyCell, LazyLockの追加

kotlinで言うところのlateinit的な奴っぽい。んだけど、LazyLock見る限り使いどころが分かんなくなった。

use std::sync::LazyLock;
use std::time::Instant;

fn main() {
    let hello = "Hello, World!".to_string();
    let lazy = LazyLock::new(|| hello.to_uppercase());

    println!("{:?}", lazy);
    println!("{}", &*lazy);

    let not_lazy = hello.to_uppercase();
    println!("not lazy: {}", not_lazy);
}

結果がこれ

LazyLock(<uninit>)
HELLO, WORLD!
not lazy: HELLO, WORLD!

cfgの名前

cfgの名前を間違ってたら教えてくれるようになった。

#[cfg(miyatama)]
mod test {
    use super::*;

    #[test]
    fn test_main() {
        assert_eq!(true, false);
    }
}

実行すると下記ワーニングが出る。

warning: unexpected `cfg` condition name: `miyatama`
  --> src/main.rs:15:7
   |
15 | #[cfg(miyatama)]
   |       ^^^^^^^^
   |
   = help: expected names are: `clippy`, `debug_assertions`, `doc`, `docsrs`, `doctest`, `feature`, `miri`, `overflow_checks`, `panic`, `proc_macro`, `relocation_model`, `rustfmt`, `sanitize`, `sanitizer_cfi_generalize_pointers`, `sanitizer_cfi_normalize_integers`, `target_abi`, `target_arch`, `target_endian`, `target_env`, `target_family`, `target_feature`, `target_has_atomic`, `target_has_atomic_equal_alignment`, `target_has_atomic_load_store`, `target_os`, `target_pointer_width`, `target_thread_local`, `target_vendor`, `test`, `ub_checks`, `unix`, and `windows`
   = help: consider using a Cargo feature instead
   = help: or consider adding in `Cargo.toml` the `check-cfg` lint config for the lint:
            [lints.rust]
            unexpected_cfgs = { level = "warn", check-cfg = ['cfg(miyatama)'] }
   = help: or consider adding `println!("cargo::rustc-check-cfg=cfg(miyatama)");` to the top of the `build.rs`
   = note: see <https://doc.rust-lang.org/nightly/rustc/check-cfg/cargo-specifics.html> for more information about checking conditional configuration
   = note: `#[warn(unexpected_cfgs)]` on by default

Rangeをいい感じに拡張

Rangeでxx未満とかxx以上とかが指定できるようになった。

fn main() {
    let my_sales = 10000;
    println!("{} class yen", size_prefix(my_sales));
}

fn size_prefix(n: u32) -> &'static str {
    const K: u32 = 10u32.pow(3);
    const M: u32 = 10u32.pow(6);
    const G: u32 = 10u32.pow(9);
    match n {
        ..K => "",
        K..M => "k",
        M..G => "M",
        G.. => "G",
    }
}

結果

k class yen

その他

Stabilized APIsで書かれている通り、IPAddrとかNonNullとかが拡張されてるっぽい

まとめ

LazyLockの使いどころがわからんのはRust力がたりないんだと思う。

2
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?