Skip to content

Commit 3f8ebad

Browse files
committed
rust: Simple rust hello_world application
Create the Rust equivalent of the hello_world application. This shows the use of printk from Rust as well as accessing a string value from Kconfig. Signed-off-by: David Brown <[email protected]>
1 parent 68e2023 commit 3f8ebad

File tree

6 files changed

+64
-0
lines changed

6 files changed

+64
-0
lines changed

samples/rust/.gitignore

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# Cargo drops lock files in projects to capture resolved dependencies.
2+
# We don't want to record these.
3+
Cargo.lock
4+
5+
# Cargo encourates a .cargo/config.toml file to symlink to a generated
6+
# file. Don't save these.
7+
.cargo/
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# SPDX-License-Identifier: Apache-2.0
2+
3+
cmake_minimum_required(VERSION 3.20.0)
4+
5+
find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE})
6+
project(hello_world)
7+
8+
rust_cargo_application()

samples/rust/hello_rust/Cargo.toml

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# Copyright (c) 2024 Linaro LTD
2+
# SPDX-License-Identifier: Apache-2.0
3+
4+
[package]
5+
# This must be rustapp for now.
6+
name = "rustapp"
7+
version = "0.1.0"
8+
edition = "2021"
9+
description = "A sample hello world application in Rust"
10+
license = "Apache-2.0 or MIT"
11+
12+
[lib]
13+
crate-type = ["staticlib"]
14+
15+
[dependencies]
16+
zephyr = "0.1.0"

samples/rust/hello_rust/prj.conf

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# Copyright (c) 2024 Linaro LTD
2+
# SPDX-License-Identifier: Apache-2.0
3+
4+
CONFIG_RUST=y

samples/rust/hello_rust/sample.yaml

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
sample:
2+
description: Hello world, but in Rust
3+
name: hello rust
4+
common:
5+
harness: console
6+
harness_config:
7+
type: one_line
8+
regex:
9+
- "Hello world from Rust on (.*)"
10+
filter: CONFIG_RUST_SUPPORTED
11+
tests:
12+
sample.rust.hellorust:
13+
tags: introduction

samples/rust/hello_rust/src/lib.rs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// Copyright (c) 2024 Linaro LTD
2+
// SPDX-License-Identifier: Apache-2.0
3+
4+
#![no_std]
5+
6+
use zephyr::printkln;
7+
8+
// Reference the Zephyr crate so that the panic handler gets used. This is only needed if no
9+
// symbols from the crate are directly used.
10+
extern crate zephyr;
11+
12+
#[no_mangle]
13+
extern "C" fn rust_main() {
14+
printkln!("Hello world from Rust on {}",
15+
zephyr::kconfig::CONFIG_BOARD);
16+
}

0 commit comments

Comments
 (0)